필터 지우기
필터 지우기

How to crop only selected part of an image automatically?

조회 수: 8 (최근 30일)
Akib Rahman
Akib Rahman 2018년 6월 21일
편집: Jyoti Verma 2019년 11월 11일
I have some image, from this image I need to identify only some portion that's mean segmentation. After identification, I need to crop this portion automatically. I found some code for automatically cropping but don't work exactly what I want! In my dataset images are different and for that reasons, segmented images also different. For example, a pre-processed image is first one and I need to detect the image in that way, an example is the second one.
Now I want to crop the right image automatically only segmented area.
In my code, it cropped incorrectly different region :(.
In my dataset images is different one from another!
My code is given below:
close all;
I=imread('CL_2_2.jpg');
figure, imshow(I);
I=rgb2gray(I);
BW=I>100;
figure,imshow(BW);
labeledImage = bwlabel(BW);
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
I2=imcrop(I,[thisBB(1),thisBB(2),thisBB(3),thisBB(4)]);
[rows cols depth]=size(I2);
if rows*cols>200
I2=imresize(I2,[28 28]);
figure,imshow(I2);
end
end

채택된 답변

Majid Farzaneh
Majid Farzaneh 2018년 6월 21일
Hi It works. But you have used imresize and have changed the segments. Also your threshold for segments sizes (200) may be large. For showing smaller objects you should take it smaller. I tried this and it worked:
close all;
I=imread('CL_2_2.jpg');
figure, imshow(I);
I=rgb2gray(I);
BW=I>100;
figure,imshow(BW);
labeledImage = bwlabel(BW);
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
I2=imcrop(I,[thisBB(1),thisBB(2),thisBB(3),thisBB(4)]);
[rows cols depth]=size(I2);
if rows*cols>100
%%%%%%%I2=imresize(I2,[28 28]);
figure,imshow(I2);
end
end
  댓글 수: 2
Akib Rahman
Akib Rahman 2018년 6월 21일
편집: Akib Rahman 2018년 6월 21일
Thanks, Majid for your answer. But when I run your code it gives me one wrong figure and that was the lower right number portion. but it is not in my region of interest. Another questions is, if I want to save this cropping figure than what should I write in my code. Thanks in advanced :)
Majid Farzaneh
Majid Farzaneh 2018년 6월 21일
You're welcome. Your algorithm is not clever enough to recognize difference between ROI and the numbers. You can try some other threshold (e.g. 150) or develop your code to understand the numbers. For saving figures check this .

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

추가 답변 (1개)

Jyoti Verma
Jyoti Verma 2019년 11월 11일
편집: Jyoti Verma 2019년 11월 11일
Croping an Image,
%Read and Display an Image,
I=imread('rec.jpg');
%rgb conversion,
J=rgb2gray(I);
subplot(221);imshow(I);title('RGB image Figure')
% Crop Image Using Crop Image Interactive Tool
[K, rect] = imcrop(J);
subplot(222);imshow(J);title('Gray image Figure')
% cropped image in matlab
I2 = imcrop(J,[12.5 8.5 411 99]);
subplot(223);imshow(I2);title('Cropped image Figure')
% BINARY CONVERSION
I3=im2bw(I2);
subplot(224);imshow(I3);title('Binary image Figure')

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by