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 Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by