How to crop specific portion of a grayscale image?

조회 수: 3 (최근 30일)
Warid Islam
Warid Islam 2019년 8월 22일
댓글: Walter Roberson 2019년 8월 23일
I have few OCT images which I want to crop. I have converted those images into grayscale images. Below are the resulting images. The part under the red box(brighter portion of the image) needs to be cropped. I tried using imcrop(). I want to create a GUI where the images could be cropped automatically by pressing a button. Using imcrop() won't allow me to automate the process as then I have to manually crop the images. I tried using the code below:
img = imread('gray4.jpg');
% % get red, green, blue channels
redC = img(:,:,1);
greenC = img(:,:,2);
blueC = img(:,:,3);
% % % mask where red channel is greater than blue channel and green channel greater than blue channel
mask = blueC > 120;
% % overlay mask into original image
finalimg = bsxfun(@times, img, cast(mask,class(img)));
% %plotting
figure
imshow(finalimg)
The result is displayed in result.jpg. Please note that result.jpg corresponds to Picture4.jpg. It could be noticed that some information under the red box in Picture4.jpg is lost which is not desirable. I think it is probably related to mask = blueC > 120 in the code above. I was wondering if it is possbile to automate the process so that any image could be cropped as desired everytime a new file is opened. Any suggestions would be appreciated. Thank you.
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 8월 22일
Please attach grey4 (the ones you attached have the red border that would throw off computation)
Warid Islam
Warid Islam 2019년 8월 22일
편집: Warid Islam 2019년 8월 22일
Hi Walter,
Please find the gray4.jpg file in the above attachment. Thank you.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 22일
img = imread('gray4.jpg');
im = imclearborder(255-imclearborder(255-rgb2gray(img)));imshow(im);
hc = sum(im,2);
mask = hc >= 10000;
fr = find(mask, 1, 'first');
lr = find(mask, 1, 'last');
cr = im(fr:lr, :);
imshow(cr)
  댓글 수: 2
Warid Islam
Warid Islam 2019년 8월 22일
Hi Walter,
This works perfectly for me. Thank you.
Walter Roberson
Walter Roberson 2019년 8월 23일
Note: the image is actually RGB in the file, but only contains gray images. It has a white border around a black border around the useful data. That two imclearborder() and the two "255-" there have to do with removing the white border and then the black border.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by