How to crop specific portion of a grayscale image?

3 vues (au cours des 30 derniers jours)
Warid Islam
Warid Islam le 22 Août 2019
Commenté : Walter Roberson le 23 Août 2019
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 commentaires
Walter Roberson
Walter Roberson le 22 Août 2019
Please attach grey4 (the ones you attached have the red border that would throw off computation)
Warid Islam
Warid Islam le 22 Août 2019
Modifié(e) : Warid Islam le 22 Août 2019
Hi Walter,
Please find the gray4.jpg file in the above attachment. Thank you.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Août 2019
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 commentaires
Warid Islam
Warid Islam le 22 Août 2019
Hi Walter,
This works perfectly for me. Thank you.
Walter Roberson
Walter Roberson le 23 Août 2019
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by