I am trying to detect red balls in a basket through thresholding . My problem is I think I have the wrong Scalar values because the result is just black.I am not sure which vector channel is the luminance,hue,saturation and brightness
enter code here
Mat onespoon= image[2];
Mat onespoonnewcolor;
Mat newspoon;
cvtColor(onespoon, onespoonnewcolor, CV_BGR2HSV);
inRange(onespoonnewcolor, Scalar(30,0,60,30), Scalar(70,7,100,70), newspoon);
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", newspoon ); // Show our image inside it.
You are converting to HSV color space. That means the first, second, and third image channels correspond to hue, saturation, and value. There is no fourth channel, so your Scalar arguments should have only three elements.
In OpenCV, hue ranges from 0 to 180, and saturation and value range from 0 to 255.
Without a posted example image, I can only guess a range for the correct hue angles. This suggests that hue values in the ranges [0, 15] and [165,180] might work well. Since red wraps around the zero-point of the spectrum, this would require two applications of cv::inRange(). For hue and saturation, I would discard low values, using the range [20,255].
Your code might then look like this, after which newspoon contains the results of both inRange passes:
cv::Mat temp1, temp2;
cv::inRange(onespoonnewcolor, cv::Scalar(0,20,20), cv::Scalar(15,255,255), temp1);
cv::inRange(onespoonnewcolor, cv::Scalar(165,20,20), cv::Scalar(180,255,255), temp2);
cv::Mat newspoon;
cv::bitwise_or(temp1, temp2, newspoon);
As pointed out in the comments, it is possible to do this with only a single call to inRange(), which is more efficient and eliminates some temporary variables:
cv::Mat newspoon;
cv::inRange(onespoonnewcolor, cv::Scalar(16,20,20), cv::Scalar(164,255,255), newspoon);
newspoon = 255 - newspoon;
Related
I have the following image.
this image
I would like to remove the orange boxes/rectangle around numbers and keep the original image clean without any orange grid/rectangle.
Below is my current code but it does not remove it.
Mat mask = new Mat();
Mat src = new Mat();
src = Imgcodecs.imread("enveloppe.jpg",Imgcodecs.CV_LOAD_IMAGE_COLOR);
Imgproc.cvtColor(src, hsvMat, Imgproc.COLOR_BGR2HSV);
Scalar lowerThreshold = new Scalar(0, 50, 50);
Scalar upperThreshold = new Scalar(25, 255, 255);
Mat mask = new Mat();
Core.inRange(hsvMat, lowerThreshold, upperThreshold, mask);
//src.setTo(new scalar(255,255,255),mask);
what to do next ?
How can i remove the orange boxes/rectangle from the original images ?
Update:
For information , the mask contains exactly all the boxes/rectangle that i want to remove. I don't know how to use this mask to remove boxes/rectangle from the source (src) image as if they were not present.
This is what I did to solve the problem. I solved the problem in C++ and I used OpenCV.
Part 1: Find box candidates
Firstly I wanted to isolate the signal that was specific for red channel. I splitted the image into three channels. I then subtracted the red channel from blue channel and the red from green channel. After that I subtracted both previous subtraction results from one another. The final subtraction result is shown on the image below.
using namespace cv;
using namespace std;
Mat src_rgb = imread("image.jpg");
std::vector<Mat> channels;
split(src_rgb, channels);
Mat diff_rb, diff_rg;
subtract(channels[2], channels[0], diff_rb);
subtract(channels[2], channels[1], diff_rg);
Mat diff;
subtract(diff_rb, diff_rg, diff);
My next goal was to divide the parts of obtained image into separate "groups". To do that, I smoothed the image a little bit with a Gaussian filter. Then I applied a threshold to obtain a binary image; finally I looked for external contours within that image.
GaussianBlur(diff, diff, cv::Size(11, 11), 2.0, 2.0);
threshold(diff, diff, 5, 255, THRESH_BINARY);
vector<vector<Point>> contours;
findContours(diff, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Click to see subtraction result, Gaussian blurred image, thresholded image and detected contours.
Part 2: Inspect box candidates
After that, I had to make an estimate whether the interior of each contour contained a number or something else. I made an assumption that numbers will always be printed with black ink and that they will have sharp edges. Therefore I took a blue channel image and I applied just a little bit of Gaussian smoothing and convolved it with a Laplacian operator.
Mat blurred_ch2;
GaussianBlur(channels[2], blurred_ch2, cv::Size(7, 7), 1, 1);
Mat laplace_result;
Laplacian(blurred_ch2, laplace_result, -1, 1);
I then took the resulting image and applied the following procedure for every contour separately. I computed a standard deviation of the pixel values within the contour interior. Standard deviation was high inside the contours that surrounded numbers; and it was low inside the two contours that surrounded the dog's head and the letters on top of the stamp.
That is why I could appliy the standard deviation threshold. Standard deviation was approx. twice larger for contours containing numbers so this was an easy way to only select the contours that contained numbers. Then I drew the contour interior mask. I used erosion and subtraction to obtain the "box edge mask".
The final step was fairly easy. I computed an estimate of average pixel value nearby the box on every channel of the image. Then I changed all pixel values under the "box edge mask" to those values on every channel. After I repeated that procedure for every box contour, I merged all three channels into one.
Mat mask(src_rgb.size(), CV_8UC1);
for (int i = 0; i < contours.size(); ++i)
{
mask.setTo(0);
drawContours(mask, contours, i, cv::Scalar(200), -1);
Scalar mean, stdev;
meanStdDev(laplace_result, mean, stdev, mask);
if (stdev.val[0] < 10.0) continue;
Mat eroded;
erode(mask, eroded, cv::Mat(), cv::Point(-1, -1), 6);
subtract(mask, eroded, mask);
for (int c = 0; c < src_rgb.channels(); ++c)
{
erode(mask, eroded, cv::Mat());
subtract(mask, eroded, eroded);
Scalar mean, stdev;
meanStdDev(channels[c], mean, stdev, eroded);
channels[c].setTo(mean, mask);
}
}
Mat final_result;
merge(channels, final_result);
imshow("Final Result", final_result);
Click to see red channel of the image, the result of convolution with Laplacian operator, drawn mask of the box edges and the final result.
Please note
This code is far from being optimal, especially the last loop does quite a lot of unnecessary work. But I think that in this case readability is more important (and the author of the question did not request an optimized solution anyway).
Looking towards more general solution
After I posted the initial reply, the author of the question noted that the digits can be of any color and their edges are not necessarily sharp. That means that above procedure can fail because of various reasons. I altered the input image so that it contains different kinds of numbers (click to see the image) and you can run my algorithm on this input and analyze what goes wrong.
The way I see it, one of these approaches is needed (or perhaps a mixture of both) to obtain a more "general" solution:
concentrate only on rectangle shape and color (confirm that the box candidate is really an orange box and remove it regardless of what is inside)
concentrate on numbers only (run a proper number detection algorithm inside the interior of every box candidate; if it contains a single number, remove the box)
I will give a trivial example of the first approach. If you can assume that orange box size will always be the same, just check the box size instead of standard deviation of the signal in the last loop of the algorithm:
Rect rect = boundingRect(contours[i]);
float area = rect.area();
if (area < 1000 || area > 1200) continue;
Warning: actual area of rectangles is around 600Px^2, but I took into account the Gaussian Blurring, which caused the contour to expand. Please also note that if you use this approach you no longer need to perform blurring or laplace operations on blue channel image.
You can also add other simple constraints to that condition; ratio between width and height is the first one that comes to my mind. Geometric properties can also be a good option (right angles, straight edges, convexness ...).
I am studying the OpenCV. Now I am vary confusing the following problem.
Here is the code:
Mat img = imread("...");
Mat imgHSV;
Mat imgThresholded;
cvtColor(img, imgHSV, COLOR_BGR2HSV);
inRange(imgHSV, Scalar(150, 50, 75), Scalar(179, 255, 255), imgThresholded);
Now, I get a processed image imgThresholded. is this imgThresolded in RGB color space or HSV color space?
As per the documentation,
void inRange(InputArray src, InputArray lowerb, InputArray upperb,
OutputArray dst)
dst – output array of the same size as src and CV_8U type
This means that for 3 channel input image the output would be a single channel image, in case of thresholding, the output is a binary image which has only white(255) and black(0) pixels and the format is CV_8U only.
It is 1 a one channel image with either 0 or 255 values.
If you want to go back to your original RGB space just do the following:
cv::Mat FinalRGB;
cv::cvtColor(imgThresholded, imgThresholded, CV_GRAY2BGR);
cv::bitwise_and(imgThresholded, img, FinalRGB);
EDIT:
As #Micka stated:
cv::Mat imgMasked;
img.copyTo(imgMasked, imgThresholded);
will do the same idea but faster.
inRange() will give binary image. Not HSV or RGB. But yeah, it will consider HSV image for computation as you have given imHSV as the input image.
The function inRange() works as follows:
imThresholded (I) is set to 255 (all 1 -bits) if imHSV (I) is
within the specified 1D, 2D, 3D, ... box and 0 otherwise.
When the lower and/or upper boundary parameters are scalars, the indexes (I) at lowerb and upperb in the above formulas should be omitted.
I have specified the histogram as
MatND skinCrCbHist =Mat::zeros(Size(256,256),CV_8UC1);
ellipse(skinCrCbHist, Point(113, 155.6), Size(283.4, 159.2), 43.0, 0.0, 360.0, Scalar(255), -1); // Using a really big ellipse to find any sort of back projection in CrCb domain.
cvtColor(src, ycrcb, CV_BGR2YCrCb); //src is input, image of a person
float crrange[]={0,255};
float cbrange[]={0,255};
const float* ranges[]={crrange,cbrange};
int channelsy[]={1,2};
calcBackProject( &ycrcb, 1, channelsy, skinCrCbHist, backproj, ranges, 255, true );
imshow("bp",backproj);
The problem i face is that backproj shows a completely black image.
When I used a normal histogram created with calcHist on a natural image, i do get some sort of backprojection. But how do i use a histogram, i create artificially, by specifying an ellipse, to get a backprojection.
If I understood your problem correctly, you could use mask with the original calcHist function.
You didn't specified which version of OpenCV you are using, so I will assume the latest 2.4.6.0. The method prototype is following (omitting defaults, and types):
calcHist(images, nimages, channels, mask, hist, dims, histSize, ranges)
The third parameter is mask. The mask means, that the function will ignore all pixels which matches zero pixels in mask. In program the mask is another image correctly setup.
Here is pseudo-code for you problem:
1) get input image
2) create matrix of same size as input of type CV_8UC1 filled with zeros
3) draw white (value 255) ellipse on the new image
4) call caclHist with the new image as mask
http://docs.opencv.org/modules/imgproc/doc/histograms.html
I cannot successfully calculate the distance transformation using OpenCV (2.3) on an ubuntu. The output is either black or a copy of the orignial image, but never as expected an greyscale image with gradients.
My code:
Mat input(Size(100,100), CV_8UC1);
circle(input, Point(50,50), 10 Scalar(255,255,255), 15, 0, 0));
Mat output(Size(100,100), CV_32FC1);
distanceTransformation(input, output, CV_DIST_L2, CV_DIST_MASK_3); //Output is completely black
//distanceTransformation(input, output, CV_DIST_L2, CV_DIST_MASK_PRECISE); //Output is a "copy" of input
imshow("in", input);
imshow("out", output);
Any suggestions?
The first call is correct, but being a distance, it's not stored as uchar. When you want to display it, OpenCV converts those float (i think) into uchars. And the result seems black.
Find the max value in the output, and then scale it to fit a grayscale image
double maxVal = findMaxDistanceSomehow();
output.convertTo( displayBuffer, CV_8UC1, 255./maxVal,0);
imshow("dist", displayBuffer);
EDIT
The first idea was correct, but you actually did not try to find maxVal! You said that by looking at the picture, instead of actually extracting it. Difference between win and fail.
So, calculate the dist transform using the precise algoritm, and then
output.convertTo( displayBuffer, CV_8UC1, 10 ,0);
Edit 2
And you must put the setTo(0) there.
I need to change the hue of some pixels of my image, but i don't know how to set them!
I converted the image in HSV with CV_BGR2HSV and now i'm cycling with a for by rows and cols...
how can I access each pixel's hue?
for setting RGB i'm using this code...
CvScalar s;
s=cvGet2D(imgRGB,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
s.val[0]=240;
s.val[1]=100;
s.val[2]=100;
cvSet2D(imgRGB,i,j,s); // set the (i,j) pixel value
You already converted your image to HSV, so the 3 layers of the image now correspond to Hue, Saturation and Value:
s.val[0] is the hue.
s.val[1] is the saturation.
s.val[2] is the value.
So go ahead and use exactly the same method as for your RGB images to get and set the pixel values.
Yes, openCV uses 180° i.e., (0°-179°) cylinder of HSV; while normally its (0°-240°) in MS paint and ideally (0°-360°). So, openCV gives you result of hue from (0°-179°).