I want to find boundaries of black region
http://i40.tinypic.com/2lbi9s9.jpg
http://i44.tinypic.com/ka4vuc.jpg
I tried different values for black, but coluld find average value so region is thresholded in both pictures
One of ranges is
inRange(src_HSV, Scalar(0, 0, 0), Scalar(180, 150, 50), src_HSV);
Another is
inRange(src_HSV, Scalar(100, 40, 140), Scalar(140, 160, 255), src_HSV);
I tried to search the Internet for values of black, but couldn't find anything suitable for this case, having different tones of black
Note that in HSV, black is defined as V=0, independently of H and S (in your case, you probably need to look for small values of V and S). I would ignore the H component.
inRange(src_HSV, Scalar(0, 0, 0), Scalar(179,50, 100), src_HSV);
for black and grey shades.
Anyways it is application specific.
Follow this link to get good insights on HSV:
Would this be an option (in RGB):
if ((red + green + blue) <= 64) {
// black
} else {
// not black
}
If not you could try HSL (hue, saturation, lightness) values and set black if lightness < 10% ...
Related
I have 2 types of image to deal. one with white background and another type with dark background. My requirement is to apply different thresholds for each type
for ex : for white back ground
(thresh, img_bin) = cv2.threshold(img, 128 , 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
for dark back ground
(thresh, img_bin) = cv2.threshold(img, 128 , 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
I am reading images using cv.imread(img,0)
I am doing morphological transformation , so i need to invert the white back ground image. but for the dark background i don't want to invert.
To expand on #nathancy's comment, you could use one of the OpenCV functions, sum or mean
For an plain old 24 bit color image, black and white are represented by
( 0, 0, 0) black
(255, 255, 255) white
Here is a sample image that looks like a page out of a book:
Now let's run some code on it
import cv2 as cv
import numpy as np
img = cv.imread('lorem_ipsum.png',cv.IMREAD_COLOR)
ret = cv.mean(img)
print(ret)
ret = cv.mean(ret)
print(ret)
ret = ret*4/3
print(ret)
ret = cv.mean(cv.mean(img))[0]*4/3
print(ret)
which gives output:
(229.78, 228.28, 228.95, 0.0)
(171.74, 0.0, 0.0, 0.0)
228.98
228.98
The first line gives us the mean of blue, green, red, and alpha channels. Second line is mean of the means. Because the first line had a zero entry, the mean of means is too low. We want to ignore alpha channel. So on the last line we pick off just the first element in mean of means and scale it by 4/3 to get a 0 to 255 answer. Our answer is 228.98 --> the picture is mostly white. The last line is the result of doing all of the operations in one line.
I want to check if a foregrond is surrounded by a certain color (int this case it's green) or if surrounded by enough pixels of a certain color.
I have the image and it's mask (below are 2 examples):
I inverted the mask, converted the image surrounding the object to HSV color space and filtered by the green color:
Rect ballBBox = boundingRect(contour);
Mat ballMask(mask, ballBBox);
Mat ballImg(img, ballBBox);
Mat imgSurroundingBall;
Mat ballMaskInv;
bitwise_not(ballMask, ballMaskInv),
ballImg.copyTo(imgSurroundingBall, ballMaskInv);
Mat imgSurroundingBallHSV;
cvtColor(imgSurroundingBall, imgSurroundingBallHSV, CV_BGR2HSV);
Scalar greenLower = Scalar(35, 100, 20);
Scalar greenUpper = Scalar(70, 255, 255);
Mat areaAroundBall;
inRange(imgSurroundingBallHSV, greenLower, greenUpper, areaAroundBall);
and the result is:
Below are examples when an object is not surrounded by green:
One of the ideas that I had was to find contour (after dialating the images) that is similar to ballMask and areaAroundBall images. But, it didn't work well since I still had a lot countours and couldn't find similar ones.
Any ideas?
I have an image stored in RGB color space and I need to detect yellow pixel and increment each one by 5.
For example, if I have a photo with a yellow lemon and a brown table, I have to turn the lemon more yellow and the table must remain the same.
Then I have to save the new image.
How can I perform it with openCV and C++?
Yes.
Convert image into HSV color space.
Calculate yellow range in HSV (from Scalar to Scalar).
Create binary mask for yellow: inRange.
Call add with mask from (3) for your HSV image and cv::Scalar(5, 0, 0)
Convert Result to RGB.
Example:
cv::Mat rgbImg = cv::imread("src.jpg", cv::IMREAD_COLOR);
cv::Mat hsvImg;
cv::cvtColor(rgbImg, hsvImg, cv::COLOR_BGR2HSV);
cv::Mat threshImg;
cv::inRange(hsvImg, cv::Scalar(20, 100, 100), cv::Scalar(30, 255, 255), threshImg);
cv::imwrite("thresh.png", threshImg);
cv::add(hsvImg, cv::Scalar(5, 0, 0), hsvImg, threshImg);
cv::cvtColor(hsvImg, rgbImg, cv::COLOR_HSV2BGR);
cv::imwrite("res.png", rgbImg);
And pictures:
Ok, I have a very, very large background image, well not an image but a rectangle colored blue:
bg2 =display.newRect(0,0,20000,20000)
bg2.y=10000
bg2:setFillColor( 0 , 0, 225)
Is it possible to make the rectangle not just one solid color, but a linear color ramp from black to blue? In other words, the color fades from black to blue vertically. I don't want to use an image, because it would be way too large.
You're looking for a gradient fill color, such as this.
local black = {0, 0, 0}
local blue = {0, 0, 1}
local g = {type="gradient", color1=black, color2=blue}
bg2:setFillColor(g)
I use CGContextStrokePath painted on a straight line in a white background picture, stroke color is red, alpha is 1.0
After drawing the line, why the points is not (255, 0, 0), but (255, 96, 96)
Why not pure red?
Quartz (the iOS drawing layer) uses antialiasing to make things look smooth. That's why you're seeing non-pure-red pixels.
If you stroke a line of width 1.0 and you want only pure red pixels, the line needs to be horizontal or vertical and it needs to run along the center of the pixels, like this:
CGContextMoveToPoint(gc, 0, 10.5);
CGContextAddLineToPoint(gc, 50, 10.5);
CGContextStroke(gc);
The .5 in the y coordinates puts the long along the centers of the pixels.