follows the code file:
Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How to detect other types of colors?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello! I am developing a project that needs to identify silver wires through colors.
I found this code but it shows a "Threshold" for each color:
%%Initialization
redThresh = 0.24; % Threshold for red detection
greenThresh = 0.05; % Threshold for green detection
blueThresh = 0.15; % Threshold for blue detection
vidDevice = webcam;
hblob = vision.BlobAnalysis('AreaOutputPort', false,'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true','MinimumBlobArea', 600,'MaximumBlobArea', 3000, ...
'MaximumCount', 10); % Set blob analysis handling
hshapeinsBox = vision.ShapeInserter('BorderColorSource', 'Input port','Fill', true, ...
'FillColorSource', 'Input port','Opacity', 0.4); % Set box handling
htextinsRed = vision.TextInserter('Text', 'Red : %2d','Location', [5 2], ...
'Color', [1 0 0],'Font', 'Courier New','FontSize', 14);% Set text for number of blobs
htextinsGreen= vision.TextInserter('Text', 'Green : %2d','Location', [5 18], ...
'Color', [0 1 0],'Font', 'Courier New','FontSize', 14);% Set text for number of blobs
htextinsBlue = vision.TextInserter('Text', 'Blue : %2d','Location', [5 34], ...
'Color', [0 0 1],'Font', 'Courier New','FontSize', 14);% Set text for number of blobs
htextinsCent = vision.TextInserter('Text', '+ X:%4d, Y:%4d','LocationSource', 'Input port', 'Color', [1 1 0], 'Font', 'Courier New','FontSize', 14);% set text for centroid
hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player
'Position', [100 100 1300 750]);% Output video player
nFrame = 0; % Frame number initialization
%%Processing Loop
while(1)
rgbFrame = snapshot(vidDevice); % Acquire single frame
rgbFrame = flipdim(rgbFrame,2); % obtain the mirror image for displaying
diffFrameRed = imsubtract(rgbFrame(:,:,1), rgb2gray(rgbFrame)); % Get red component of the image
diffFrameRed = medfilt2(diffFrameRed, [3 3]); % Filter out the noise by using median filter
binFrameRed = im2bw(diffFrameRed, redThresh); % Convert the image into binary image with the red objects as white
diffFrameGreen = imsubtract(rgbFrame(:,:,2), rgb2gray(rgbFrame)); % Get green component of the image
diffFrameGreen = medfilt2(diffFrameGreen, [3 3]); % Filter out the noise by using median filter
binFrameGreen = im2bw(diffFrameGreen, greenThresh); % Convert the image into binary image with the green objects as white
diffFrameBlue = imsubtract(rgbFrame(:,:,3), rgb2gray(rgbFrame)); % Get blue component of the image
diffFrameBlue = medfilt2(diffFrameBlue, [3 3]); % Filter out the noise by using median filter
binFrameBlue = im2bw(diffFrameBlue, blueThresh); % Convert the image into binary image with the blue objects as white
[centroidRed, bboxRed] = step(hblob, binFrameRed); % Get the centroids and bounding boxes of the red blobs
centroidRed = uint16(centroidRed); % Convert the centroids into Integer for further steps
[centroidGreen, bboxGreen] = step(hblob, binFrameGreen); % Get the centroids and bounding boxes of the green blobs
centroidGreen = uint16(centroidGreen); % Convert the centroids into Integer for further steps
[centroidBlue, bboxBlue] = step(hblob, binFrameBlue); % Get the centroids and bounding boxes of the blue blobs
centroidBlue = uint16(centroidBlue); % Convert the centroids into Integer for further steps
rgbFrame(1:50,1:90,:) = 0; % put a black region on the output stream
vidIn = step(hshapeinsBox, rgbFrame, bboxRed, single([1 0 0])); % Instert the red box
vidIn = step(hshapeinsBox, vidIn, bboxGreen, single([0 1 0])); % Instert the green box
vidIn = step(hshapeinsBox, vidIn, bboxBlue, single([0 0 1])); % Instert the blue box
for object = 1:1:length(bboxRed(:,1)) % Write the corresponding centroids for red
centXRed = centroidRed(object,1); centYRed = centroidRed(object,2);
vidIn = step(htextinsCent, vidIn, [centXRed centYRed], [centXRed-6 centYRed-9]);
end
for object = 1:1:length(bboxGreen(:,1)) % Write the corresponding centroids for green
centXGreen = centroidGreen(object,1); centYGreen = centroidGreen(object,2);
vidIn = step(htextinsCent, vidIn, [centXGreen centYGreen], [centXGreen-6 centYGreen-9]);
end
for object = 1:1:length(bboxBlue(:,1)) % Write the corresponding centroids for blue
centXBlue = centroidBlue(object,1); centYBlue = centroidBlue(object,2);
vidIn = step(htextinsCent, vidIn, [centXBlue centYBlue], [centXBlue-6 centYBlue-9]);
end
vidIn = step(htextinsRed, vidIn, uint8(length(bboxRed(:,1)))); % Count the number of red blobs
vidIn = step(htextinsGreen, vidIn, uint8(length(bboxGreen(:,1)))); % Count the number of green blobs
vidIn = step(htextinsBlue, vidIn, uint8(length(bboxBlue(:,1)))); % Count the number of blue blobs
step(hVideoIn, vidIn); % Output video stream
nFrame = nFrame+1;
end
%%Clearing Memory
release(hVideoIn); % Release all memory and buffer used
release(vidDevice);
clear all;
clc;
I want to detect other kind of colors like: yellow, lime green, orange, purple, hot pink. But I do not know the "Threshold" for these colors according to the code.
Please, who can help me because I am a layman, I thank you very much!
댓글 수: 1
답변 (1개)
Image Analyst
2018년 5월 13일
You didn't show your image so it's hard to advise you. Basically if you have a megapixel image, you could potentially have anywhere from one up to a million colors. You might want to try rgb2ind() to use one algorithm that quantifies colors into a specified number of simple colors. Or you could use KNN to classify colors into the closest color from a training set of colors. See attached.
댓글 수: 16
Image Analyst
2018년 5월 14일
I'd take any of the color channels and run stdfilt() on it to identify uniform areas. Those will have a standard deviation of zero. Once you have those regions identified, call regionprops for each color channel and ask for 'MeanIntensity' to get the RGB values of each uniform region.
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!