convert the image to binary

6 views (last 30 days)
Md
Md on 21 Sep 2022
Commented: Image Analyst on 21 Sep 2022
I need to convert the first image to the second image. I posted about this before but the answers didn't help. One of my classmate suggested me to do this (he said that repeat bwareopen and medfilt2 a few time at the end, no specific number), but it didn't help:
RGB=imread("center.jpg");
a=medfilt3(RGB);
b=rgb2gray(a);
c=medfilt2(b);
d=imbinarize(c);
e=medfilt2(d);
imshow(e);
f=bwareaopen(e,1);
g=medfilt2(f);
h=bwareaopen(g,1);
i=medfilt2(h);
imshow(i);
j=bwareaopen(i,1);
k=medfilt2(j);
l=bwareaopen(k,1);
m=medfilt2(l);
Can I get any suggestions? Thank you!
  2 Comments
Walter Roberson
Walter Roberson on 21 Sep 2022
I need to convert this image to a clear binary image
Your code already converts to a "clear binary image" -- at least at least as much as you have defined "clear". Which is to say that since you did not indicate how "clear" is to be determined, any binary image satisfies the requirement.
Md
Md on 21 Sep 2022
I need the image to be like this.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 21 Sep 2022
Moved: Image Analyst on 21 Sep 2022
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1131025/image.jpeg';
RGB = imread(filename);
BW = all(RGB<64, 3);
imshow(BW)
center_only = bwareafilt(BW, 1);
imshow(center_only)
center_select = RGB .* uint8(center_only(:,:,[1 1 1]));
imshow(center_select)
That last image does not look like much, but it is the RGB of only the center line, with everything else set to 0. Maybe if we try
center_select2 = imcomplement(RGB) .* uint8(center_only(:,:,[1 1 1]));
imshow(center_select2)
This is an RGB image in "reverse color" in the center. Because the center that you are interested in happens to be dark.
Your target image has grayscale end-caps, but your source image has no end-caps. This last image, center_select2 would pull out grayscale if it meaningfully existed there.
  2 Comments
Md
Md on 21 Sep 2022
Moved: Image Analyst on 21 Sep 2022
Thanks a lot!
Image Analyst
Image Analyst on 21 Sep 2022
@Md Please click the "Accept this answer" link to award @Walter Roberson reputation points. Thanks in advance. 🙂

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!