필터 지우기
필터 지우기

How to crop image with nonlinear cropping shape?

조회 수: 3 (최근 30일)
Ivan Shorokhov
Ivan Shorokhov 2015년 2월 16일
댓글: Image Analyst 2015년 2월 16일
Dear all,
I have following image:
And would like to crop it as follows:
Here is the code I'm using right now is:
clc;close all;clear all;
corn=imread('After_phi_o-phi-f.jpg');
mm_corn=imerode(corn,strel('disk',1));
bw_corn=im2bw(mm_corn, graythresh(mm_corn));
cc_corn = bwconncomp(bw_corn);
aba_corn = [cellfun(@numel,cc_corn.PixelIdxList)];
[mv_corn,ind] = sort(aba_corn,'descend');
L_corn=labelmatrix(cc_corn);
ki_corn = find(aba_corn >= mv_corn(2));
mbi_corn = ismember(L_corn, ki_corn);
bw_corn(~mbi_corn) = 0;
Ibw = imfill(bw_corn,'holes');
Ilabel = bwlabel(Ibw);
stat = regionprops(Ilabel,'centroid');
imshow(bwconvhull(im2bw(corn, graythresh(corn)))); hold on;
plot([stat(1).Centroid(1),stat(2).Centroid(1)], [stat(1).Centroid(2),stat(2).Centroid(2)], 'r');1)
And result is:
So I want cut information from the left until the red line, how I can do it?
Thanks for any help.
  댓글 수: 4
Image Analyst
Image Analyst 2015년 2월 16일
Images MUST be rectangular. Period.
Ivan Shorokhov
Ivan Shorokhov 2015년 2월 16일
I don't want to change shape of the image, I want to create a mask with such shape.

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

채택된 답변

Image Analyst
Image Analyst 2015년 2월 16일
Get the horizontal and vertical profiles using sum(), and get the top and bottom row and left and right column using find()
horizontalProfile = sum(binaryImage, 1);
verticalProfile = sum(binaryImage, 2);
topRow = find(verticalProfile, 1, 'first');
bottomRow = find(verticalProfile, 1, 'last');
leftColumn= find(horizontalProfile, 1, 'first');
rightColumn = find(horizontalProfile, 1, 'last');
croppedImage = binaryImage(topRow:bottomRow, leftColumn:rightColumn);
  댓글 수: 2
Ivan Shorokhov
Ivan Shorokhov 2015년 2월 16일
@Image Analyst, you showed an alternative way of cropping image, BUT I have asked my question incorrectly. So my actual question is: how to make nonlinear cropping shape, for example as such:
______
/______|
So I will get result will looks like this:
Original:
Cropped:
Image Analyst
Image Analyst 2015년 2월 16일
Is the line always red? And does it never extend to the boundaries of the blob? If so I'd suggest you extract the red line, then find the endpoints of it with bwmorph(), then draw a black line in the image using imline(), demo attached. Then extract the biggest blob, demo attached.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by