MATLAB Answers

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

4 views (last 30 days)
kanika bhalla
kanika bhalla on 7 Jul 2021
Commented: kanika bhalla on 8 Jul 2021
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
kanika bhalla
kanika bhalla on 7 Jul 2021
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)

kanika bhalla
kanika bhalla on 7 Jul 2021
Edited: kanika bhalla on 8 Jul 2021
@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
kanika bhalla
kanika bhalla on 8 Jul 2021
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.

Community Treasure Hunt

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

Start Hunting!