Eliminating the Border Around a Generated Image

I am creating images using the rectangle() function with various line and space sizes. I attached an example image to this question. I want to eliminate the border of white that surrounds the black rectangles. (It affects my pixel count calculations.) Currently, I am just using the imcrop() function, but the cropping dimensions have to change according to the line and space sizes. Is there a more efficient way than imcrop()?

 채택된 답변

jonas
jonas 2018년 8월 27일
편집: jonas 2018년 8월 27일

0 개 추천

There is a function that will do this for you in one step, but I forgot what it's called. Here's a primitive solution for you!
[y1,x1]=find(BW==0,1,'first')
[y2,x2]=find(BW==0,1,'last')
BW2=BW(y1:y2,x1:x2)
imshow(BW2)
Basically you find the upper left and bottom right corners, and then crop a rectangle between those.

댓글 수: 5

Sarah
Sarah 2018년 8월 27일
편집: Image Analyst 2018년 8월 27일
Thanks! However, I end up with an image that repeats the image 3 times in a row. Do you know why this is and how to fix it? I've attached the image. (The workaround I implemented was to make x2 = y2, but I don't think that's a good longterm solution.)
Image Analyst
Image Analyst 2018년 8월 27일
편집: Image Analyst 2018년 8월 27일
Is your BW really an RGB image instead of a logical (binary) image? What does this show
[rows, columns, numberOfColorChannels] = size(BW) % Don't use a semicolon
[rows, columns, numberOfColorChannels] = size(IM)
rows =
420
columns =
560
numberOfColorChannels =
3
Yep. 3 color channels - it's RGB. Try this first
BW = BW(:, :, 2); % Take green channel.
or else, if you want to keep it as a color image, do this:
BW2 = BW(y1:y2, x1:x2, :)
Sarah
Sarah 2018년 8월 27일
Excellent, thank you. Now it all works well.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2018년 8월 27일

댓글:

2018년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by