image analysis to find pH strip and determine pH

조회 수: 8 (최근 30일)
Brandon Kellems
Brandon Kellems 2021년 12월 2일
댓글: Image Analyst 2021년 12월 3일
Hello, I need help with a program to be able to locate a pH strip in an image. The position of the strip will change with each run of the code. I have the color part completed. I just do not know where to start to be able to find the pH strip. After the pH strip is located, I need to be able to take the RGB values of a pixel on the pH strip (preferably the center of the strip). I know how to take the RGB values of a point but I do not know how to define this point in relation to the changing position of the pH strip.
This picture is an example of how the strip will look in one run, the position will change each run.

답변 (3개)

Image Analyst
Image Analyst 2021년 12월 2일
Not sure I understand. You say "I have the color part completed" so I assume that means you have located the colored strip in the image and are able to get its mean RGB values. Like for example you used the ColorThresholder app to find non-neutral colored pixels in the image, or you used stdfilt() to identify non-smooth regions, or you used background correction to find out what is different have the user put something into the field of view. So like you have a binary image of where the strip is. So what does it mean when you say "I do not know how to define this point in relation to the changing position of the pH strip."?
Two questions:
  1. Why don't you have a jig to put the strip into so it's always in the same location and in focus and you don't have to worry about finding non-strip things like fingers?
  2. Why don't you have a Color Checker calibration chart in the image to calibrate your scene? www.calibrite.com

DGM
DGM 2021년 12월 2일
편집: DGM 2021년 12월 2일
Not knowing what any of the other images look like or what other colors need to be accomodated, it's kind of hard to make anything robust. Here's something that's a start. You'll almost certainly have to adjust the thresholds as needed. Without better scene management, this may be a significant problem.
rgbpict = imread('phstrip.jpg');
hsvpict = rgb2hsv(rgbpict);
Smask = hsvpict(:,:,2)>0.05 & hsvpict(:,:,2)<0.3;
Vmask = hsvpict(:,:,3)>0.70 & hsvpict(:,:,3)<0.8;
mask = imfill(bwareafilt(Vmask & Smask,1),'holes');
mask = imerode(mask,strel('disk',5)); % keep away from marginal areas
% show masked region
imshow(im2double(rgbpict).*mask)
% extract mean color tuple of ROI
roipix = rgbpict(repmat(mask,[1 1 3]));
meancolor = mean(reshape(im2double(roipix),[],3),1)
meancolor = 1×3
0.6361 0.7580 0.6036
% show color swatch
figure
imshow(permute(meancolor,[1 3 2]))
  댓글 수: 2
Brandon Kellems
Brandon Kellems 2021년 12월 2일
편집: Brandon Kellems 2021년 12월 2일
This code looks like it will be able to find the strip very easyily, especially since it returns the mean color. The colours it should accomodate for are the base pH levels. This picture is a little blurry. In regards to your response, you used the phrase "scene management", by this do you mean the background of the image with the pH strip? Also thank you for your help so far. Also, is there a way to make the color swatch bigger or am I able to get the rgb from that size?
DGM
DGM 2021년 12월 2일
편집: DGM 2021년 12월 2일
When I say "scene managmement", I'm referring to the whole of things that influence the character of the image. This might include object/camera fixturing for repeatable object position within the frame and repeatable subject distance so that a manual focus setting can be used. It might include efforts to ensure uniform illumination and good subject/background contrast without swamping the camera with excessively bright areas. It would involve exposure control so that images are comparable. Image Analyst touched on this. The goal is to make the images consistent and as easy to process as possible.
The chart image might be usable, but unless these two images were taken under the same conditions, I'm not sure how well that would work. I'm not familiar with trying to get calibrated images out of cameras, especially if the exposure, etc can't be fixed.
Without any color adjustments, this is the colortable and the presumed graduations. Using the mean color of the strip from the prior example, the pH can be estimated by distance minimization, though given how extremely close many of the CT entries are, this may be problematic. Consider that in this case, the distance between meancolor and the nearest CT entry is 0.0467. To put that in perspective, the distance between the neighboring entries in CT is as small as 0.0001.
CT = [0.8377 0.1622 0.226;0.892 0.1889 0.2285;0.9477 0.1903 0.2407;0.9539 0.2871 0.2588;0.942 0.4929 0.3693;
0.965 0.5415 0.3923;0.9816 0.5897 0.4206;0.9789 0.6252 0.4228;0.9939 0.6547 0.4487;0.9809 0.6432 0.4379;
0.9607 0.6383 0.4296;0.9022 0.6952 0.4668;0.8192 0.7257 0.4936;0.359 0.5953 0.4375;0.08396 0.5178 0.3804;
0.07511 0.4413 0.327;0.1081 0.3433 0.2732;0.1106 0.3028 0.2545;0.1175 0.2679 0.2448;0.1251 0.2675 0.2688;
0.1281 0.2552 0.2942;0.1325 0.2385 0.2717;0.1383 0.227 0.2684;0.1218 0.2057 0.2515;0.1106 0.1725 0.2256;
0.1133 0.1424 0.1795;0.09379 0.1225 0.1589;0.09615 0.1314 0.1656;0.1054 0.1237 0.1397];
% chart numbers are unreadable. this is just a guess
phref = linspace(0,14,29);
[~,idx] = min(sum((CT-meancolor).^2,2));
phref(idx) % estimated pH
Unless the goal of this effort is to specifically perform this task by image analysis, it may be a lot easier and more consistent to get an inexpensive pH meter (depending on the range of what you need to measure).

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


Image Analyst
Image Analyst 2021년 12월 2일
I agree with @DGM, you'd need to have your chart and strip both taken under the same conditions. And using an instrument built already for this might be more accurate and easier than developing an image analysis app to do it. Though maybe you're just doing it via image analysis for fun or a school project.
That said, a simple way might be to read the mean RGB of all the reference chips and get them into a 28 row by 3 column array.
Then once you have the color of the unknown strip, simply compute the color difference in RGB space like
colorDistances = sqrt((testR - refRGB(, 1)) .^ 2 + (testG - refRGB(, 2)) .^ 2 + (testB - refRGB(, 3)) .^ 2);
Or you could have your reference colors in 3 separate vectors for R, G, and B of course.
Now find the chip that's closest to the test color
[minDiff, chipIndex] = min(colorDistances);
Whatever chipIndex is, that's the Ph that the test strip is.
If your image capture conditions are not the same, I'd first try to do everything you can do to make them the same and if you still need more accuracy, you can do calibrated color imaging where are the colors are basically traceable back to a spectrophotometer (the gold standard for color measurement). I have lots of experience (decades) doing this. You'll need a reference chart like provided by :
  댓글 수: 4
Brandon Kellems
Brandon Kellems 2021년 12월 3일
yes, I am worried that using image analysis would be more difficult, especially because the background may be weird (changing frequently). I am using an external webcam. How do I add the overlay and take the point at the crosshairs? I tried to use the get points function but I kept messing it up.
Image Analyst
Image Analyst 2021년 12월 3일
It's something like (untested)
cam = webcam
preview(cam);
% Snap an image to we can get the size of it.
img = snapshot(cam);
[rows, columns, numberOfColorChannels] = size(img)
% Display cross hairs.
xline(columns/2, 'Color', 'r', 'LineWidth', 2);
yline(rows/2, 'Color', 'r', 'LineWidth', 2);
Then to take a picture
%Close the preview at any time using the closePreview function.
closePreview(cam)
% If you do not explicitly close the preview, it closes when you clear the webcam object.
% Acquire a single image from the camera using the snapshot
% function and assign it to the variable img.
img = snapshot(cam);
% Display the acquired image.
imshow(img)

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

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by