How to obtain threshold image separating two colors using same approach for all different kind of attached images.

I am working on an industrial project where my task is to automatically generate two paths that can connect the conduction wire from left to right sides as shown in attached Figure 1. I have successsfuly achieved the target and obtained the lines automatically. But I want to run my code on all images. So, I got stuck on one part to make it applicable on all images without manually changing any input values.
I want to separte two colors (as shown in cropped figures). I have automatically generated cropped images so that i will be left with only two colors to separte. And I can separte these two colors using RGB based thresholding, HSV segmentation, LAB, color difference, otsu method, thresholding based approaches. In the below mentioned code I have done RGB based thresholding to obtain the threshold image. But the problem I am facing is that I need to threshold all images using same approach and same value. And these approaches gives desired result only by changing different values for different images. And I am having sround 1000s of images.
As I can observe and checked that background (Brown in color) is having different values than green part. But I am not able to remove that part and only extract green boundaries. As I am learning Matlab programming step by step. So, I am not able to achieve this task. I have attached few original and cropped images for your reference.
Can anyone please help me to achieve my task. As I am not able to think any of the methods and got badly stuck in this part and rest is all done.
Thank you for your time and support!!
OriginalPicture = imread('D:\test\14H17\18.jpg'); %input image
CroppedPicture=imreda('cropped')% Input cropped picture with only two colors
binaryImage = R > 130 & R < 150 & G > 150 & G < 180 & B > 110 & B < 150; % seprate gray color from green to get boundaries

2 Comments

"I have automatically generated cropped images so that i will be left with only two colors to separte." How is that true? From what I see in the tiny little sub images, there are more than 2 colors. Why do you say that you generated cropped images with only 2 colors?
Thank you so much for your reply Sir @Image Analyst
I am really sorry as I havn't added whole code. My mistake. As it is little difficult to add 2 separte files to describe how I have obtained this cropped images automatically. But here I have tried to explain it clearly what I have done. Please correct me if I am still wrong.
Firstly I have obtained segemnted defect part. And then automatially drawn rectangle around segemented defect. After that I have extended the rectangle so that I will be left with less part to deal and cropped it. And then I made defect part within a rectangle zero so that I will be left with only two colors two seperate.
Thank you for your time and help.
OriginalPicture = imread('D:\test\14H17\18.jpg'); %input image
segmenteddefect=imread('segmenteddefect'); % I have obtained this segmented defect automatically. As it is little difficult for me two add two functions file to describe how I have obtained this segmented defect automatically.
figure; imshow(segmenteddefect)
hold on
for k = 1 : length(info)
BB = info(k).BoundingBox;
rectangle('Position', [BB(1),BB(2),BB(3),BB(4)],'EdgeColor','r','LineWidth',2) ; % to draw rectangle around obtained segemnted defect contour.
x= BB(1)
y=BB(2)
width= BB(3)
height= BB(4)
end
hold off;
% Step 2: I have extended a rectangle so that I will be left with less
% colors to deal wih.
figure; imshow(Image); % draw rectangles around colored input OriginalPicture
x= BB(1)
y=BB(2)
width= BB(3)
height= BB(4)
hold on;
rectangle('Position',[x,y,width,height],...
'EdgeColor', 'r',...
'LineWidth', 3,...
'LineStyle','-')
hold off;
x1 = ceil(x);
x2 = round(x1 + width);
y1 = ceil(y);
y2 = round(y1 + height);
% Step 3: I want to get only two colors to deal with. Therefore I have made
% rectangle containg defect part zero
Image(y1:y2,x1:x2,:) = 0
figure; imshow(Image)
% step 4: to draw biggere rectangle
x11=x1-24
y11=y1-16
width11=x2-410
height11=y2-275
hold on;
h1=rectangle('Position',[x11,y11,width11,height11],...
'EdgeColor', 'y',...
'LineWidth', 3,...
'LineStyle','-')
% step 5: crop the yellow rectangle object so that i will be left with only
% two colors.
I2 = imcrop(Image,[x11 y11 width11 height11]);
figure; imshow(I2);
%step 7: RGB based thresholding
% [rows, columns, numberOfColorChannels] = size(I2)
R= I2(:,:,1); % take the red channel of Crop_image
G=I2(:,:,2); % take the green channel of Crop_image
B=I2(:,:,3); % take the blue channel of Crop_image
hold on;
mask = R > 130 & R < 150 & G > 150 & G < 180 & B > 110 & B < 150;
figure; imshow(mask)
%step8: to remove extra unwanted part
mask(:, 28 : round(col_im/2), :) = 0;
figure; imshow(mask);

Sign in to comment.

Answers (1)

@Image Analyst @Walter Roberson @KALYAN ACHARJYA @Matt J @Adam Danz @Doug Hull @Ameer Hamza @Yair Altman. Could you please help me in this. I will be very grateful to you.

12 Comments

I see you put a lot of effort into the question, but I do not understand what you are trying to do, at all.
Thank you so much Sir @Doug Hull for your reply. I am realy sorry for my bad explanation.
I am just looking for any approach which will seperate two colors and give me green line thresholded boundaries as shown in Figure.
As I have around 1000s of images which and some are shown below for your reference. So, I want to use the same approach to threshold all of the images with the same method. This is because my process should be automatic to be applicable on all images at same time. Is there any way to threshold or remove the background which is different from the green part. Hope my explanation is clear. Please correct me if this time also I am not clear.
Thank you so much for your time and support.
Sorry, I do not presently have the resources to work on this topic.
Programming is the art of breaking down problems into smaller and smaller pieces that can be solved easier.
You have called upon some really great contributors to this forum, but are unable to communicate to any of us what you want to do. I suspect this is root of the problem you face.
If you can not clearly define the problem in literal natural language to us, then there is no chance you can explain it to MATLAB in the form of code.
I highly suspect this problem is solvable in MATLAB, but I do not think any of us know what that problem is.
Sorry, I would like to give input.
Thank you so much Sir @Doug Hull for your patience and support. I understand that is the biggest problem of mine. I am trying to improve this. I have tried to explain my problem statement. Please correct me if still I m unable to explain it clearly.
I am working on a real time industrial project of Automated visula inspection of defect Panel images. Here my task is to automatically check if anything is present on green glasses (I have shown in attached figure) that is considered to be a defect. And after that we need to do laser cutting on green glasses. For this I need to automatically generate two paths ( as shown in target image) that can connect the conduction wire from the left to the right sides.
So, to achieve this target I have divided my problem into sub parts as described below:
Step 1: I have automatically segemnted the defect part from the original image. This is done to automatically generate the rectangles around the defect contour. Then I have automatically extended rectangle so that I can crop the part that will contain only glass part and background.
Step 2: And then I have obatined the same rectangles on color image. After that image within rectangles are cropped. Then rectangle around defect contour is filled with zeros. The idea was to get an image with less colors to threshold.Now I am left with an image with glasses part and background as shown in Figures.
I have done this step because as we can clearly observe it is left with green glasses and background. Now I want to thrsehold this image so that I will get green glasses threshold boundaries as shown below:
I have obtained this threshold image by RGB based thresholding. Likewise I want to thrsehold result of my images. But I am unable to get any method that will solve my problem. As all process should be automatic and applicable to all 1000s of images at same time.
Step 3: Now I have thresholded the glass part by manually checking the RGB values of glasses and background. So, I got stuck in this part because I have manually checked the RGB values for the attached figure and got the results. But in other cases I have to manually change the RGB values to threshold glass part.
Step 4: After that I have automatically calculated the end points to draw line between them. But I got stuck in step 3 to apply this approach on all images.
So I am looking for an approach which will threshold only glass part so that I will calculate the end points to draw lines between them as shown in Figure.
Please help me in this. As I have tried many methods but unable to solve this problem of thresholding glass part. Is there any approach by which i can remove background part for all images.
Thank you so much for your time and patience.
How about this:
Give us a clear input image.
Then give us a hand modified image each step along the way. Hand draw what you want.
I have read this many times, I am starting to have an idea, but I do not really understand what you are trying to do, at all.
This is a communication issue, and I just have very little idea what your process is.
Looks to me like he has water stains on a map and he wants to do some sort of automated reading/classification of routes/roads/lines on them and the water stains are messing everything up.
Thank you so much Sir @Doug Hull for your patience, time and support. I am really grateful that great people are helping me to solve my problem inpite of my unclear explanation. I have again tried to explain what I want to achieve. Please correct me if I am still not clear with my explanations.
Answering your first question This is an input LCD Panel Defect image.
And my target is to draw lines on the image as shown below:
And I have already drawn these lines automatically as shown in above Figure. But I have to draw these lines on other images too automatically using the same code. Therefore, I got stuck on one step to make my method applicable on all images. And now I am going to describe step by step what I want to achieve.
I want to thrsehold the the green part to get boundaries. I have shown with example what I am trying to do.
I have thresholded this image based on RGB values. So, this is not exact result. But I just want boundaries of green part somewhat like the one shown in above Figure.
Here is another example.
Here is an example 3:
Here is example 4:
Example 5:
And I have tried many methods like LAB, HSV segmentation, color difference. Otsu method, thresholding. These methods are giving results but on some images and I have to manually change the threshold value. So, I am looking for an approach which will be applicable to my all images.
Is there any method based on histogram that will remove my background part and gives green part threshold image or any other approach to achieve my task.
Because right now I am able to generate path thta is draw lines only on one image. Because for other images i need to change threshold.
If you want me to add code that how i have drawn lines on the original image, I will happily add and explain step by step.
Please help me in this. I am badly stuck with this part.
And here I attach the step by step procedure what I have done to attain lines automatically. Below steps are not manually attained. I have done those automatically. Therefore, I am looking for a way that can threshold green part so that I will draw lines on all images.
Thank you so much Sir @Image Analyst for your time, patience and help. Answering to your question - Actually it is a LCD Panel images. And there are some kind of defects on it as you mentioned water stains. And company wants to automatically draw lines on the defect part which is present on green glasses. Otherwise it will lead to short circuit.
With the help of great people present here in forum. I have already achieved my task. Many thanks to you and matlab community members. I just got stuck one part that is how to thrsehold green glasses to apply my method on rest of images.
Please help me with this.
OK, I have a much clearer idea what you are trying to do.
>>With the help of great people present here in forum. I have already achieved my task.
And with this it looks like you have done this.
What is unclear to me now, is where are the images where this is going wrong?
You are using terminology like "green glass" many if us are not subjuect matter experts in your field. So, this does not mean anything to me.
I am really sorry again for my unclear explanations @Doug Hull.
I have attached one figure which describes the green glasses part. Here green colour parts are basically glasses in all the images. Am I clear now in explaining.
I have attached the figures in earlier comment which are wrong. As I want to threshold the images basically green glasses. And I have marked in images in previous comments.
Please correct me if I am still not clear. I am really sorry for wasting your precious time.

Sign in to comment.

Products

Release

R2019a

Asked:

on 7 Jul 2021

Commented:

on 8 Jul 2021

Community Treasure Hunt

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

Start Hunting!