Calculating image brightness using BGR color and B channel - opencv

I was trying to calculate if an image is bright or dark using mean value and by mistake I forgot to convert my BGR image to LAB and extract the LAB channel. Instead I extracted the B channel in BGR image. What surprised me when I tested this method in my labeled images it shows high accuracy, recall and precision. So when I tried to show the image to check what color it is, it shows a grayscale image (I'm not sure why I expected an image with more blue color). When I read about BGR and calculating brightness of an image it always mentioned calculating the mean value for all the channels. So how is this possible?
The images are of underwater environment
img = cv2.imread("image.jpg")
#conv_image = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_channel, _, _ = cv2.split(img)
img = l_channel
cv2.imshow("Result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Related

What does cv2.COLOR_GRAY2RGB do?

I happened to encounter this API cv2.COLOR_GRAY2RGB. I found it strange because there should have no way to convert an grey scale image to RGB image. So I tried something like this:
I took an image like this:
The image is shown by plt.imshow(img) (with default arguments).
Then I convert it into grey scale with cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) and get this:
I know it does not appear grey-scale looking is because imshow() by default is not displaying grey-scale image (more like heat-map I think). So I used cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) and got this:
It appears grey to out eyes despite it has three channels now. So I conclude that cv2.COLOR_GRAY2RGB is a walk-around to display grey-scale image in grey-scale fashion without changing the settings for imshow().
Now my question is, when I use cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) again to convert this three-channel gray image back to one channel, the pixel value is exactly the same as the first time I converted the original image into one channel with cv2.cvtColor(img, cv2.COLOR_BGR2GRAY):
In other words, cv2.COLOR_BGR2GRAY can do a many-to-one mapping. I wonder how is that possible.
COLOR_BGR2GRAY color mode estimates a gray value for each pixel using a weighted sum of B, G, R channels, w_R*R+w_G*G+w_B*B=Y per pixel. So any 3 channel image becomes 1 channel.
COLOR_GRAY2BGR color mode basically replaces all B, G, R channels with the gray value Y, so B=Y, G=Y, R=Y. It converts a single channel image to multichannel by replicating.
More documentation about color modes are here.

Segmentation problem for tomato leaf images in PlantVillage Dataset

I am trying to do segmentation of leaf images of tomato crops. I want to convert images like following image
to following image with black background
I have reference this code from Github
but it does not do well on this problem, It does something like this
Can anyone suggest me a way to do it ?
The image is separable using the HSV-colorspace. The background has little saturation, so thresholding the saturation removes the gray.
Result:
Code:
import numpy as np
import cv2
# load image
image = cv2.imread('leaf.jpg')
# create hsv
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# set lower and upper color limits
low_val = (0,60,0)
high_val = (179,255,255)
# Threshold the HSV image
mask = cv2.inRange(hsv, low_val,high_val)
# remove noise
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel=np.ones((8,8),dtype=np.uint8))
# apply mask to original image
result = cv2.bitwise_and(image, image,mask=mask)
#show image
cv2.imshow("Result", result)
cv2.imshow("Mask", mask)
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The problem with your image is the different coloration of the leaf. If you convert the image to grayscale, you will see the problem for the binarization algorithm:
Do you notice the very different brightness of the bottom half and the top half of the leaf? This gives you three mostly uniformly bright areas of the image: The actual background, the top-half leaf and the bottom-half leaf. That's not good for binarization.
However, your problem can be solved by separating your color image into it's respective channels. After separation, you will notice that in the blue channel the leaf looks very uniformly bright:
Which makes sense if we think about the colors we are talking about: Both green and yellow have very small amounts blue in it, if any.
This makes it easy for us to binarize it. For the sake of a clearer image, i first applied smoothing
and then used the iso_data Threshold of ImageJ (you can however use any of the existing automatic thresholding methods available) to create a binary mask:
Because the algorithm has set the leaf to background (black), we have to invert it:
This mask can be further improved by applying binary "fill holes" algorithms:
This mask can be used to crop the original image to extract the leaf:
The quality of the result image could be further improved by eroding the mask a little bit.
For the sake of completeness: You do not have to smooth the image, to get a result. Here is the mask for the unsmoothed image:
To remove the noise, you first apply binary fill holes, then binary closing followed by binary erosion. This will give you:
as a mask.
This will lead to

Convert image to grayscale with custom luminosity formula

I have images containing gray gradations and one another color. I'm trying to convert image to grayscale with opencv, also i want the colored pixels in the source image to become rather light in the output grayscale image, independently to the color itself.
The common luminosity formula is smth like 0.299R+0.587G+0.114B, according to opencv docs, so it gives very different luminosity to different colors.
I consider the solution is to set some custom weights in the luminosity formula.
Is it possible in opencv? Or maybe there is a better way to perform such selective desaturation?
I use python, but it doesnt matter
This is the perfect case for the transform() function. You can treat grayscale conversion as applying a 1x3 matrix transformation to each pixel of the input image. The elements in this matrix are the coefficients for the blue, green, and red components, respectively since OpenCV images are BGR by default.
im = cv2.imread(image_path)
coefficients = [1,0,0] # Gives blue channel all the weight
# for standard gray conversion, coefficients = [0.114, 0.587, 0.299]
m = np.array(coefficients).reshape((1,3))
blue = cv2.transform(im, m)
So you have custom formula,
Load source,
Mat src=imread(fileName,1);
Create gray image,
Mat gray(src.size(),CV_8UC1,Scalar(0));
Now in a loop, access BGR pixel of source like,
Vec3b bgrPixel=src.at<cv::Vec3b>(y,x); //gives you the BGR vector of type cv::Vec3band will be in row, column order
bgrPixel[0]= Blue//
bgrPixel[1]= Green//
bgrPixel[2]= Red//
Calculate new gray pixel value using your custom equation.
Finally set the pixel value on gray image,
gray.at<uchar>(y,x) = custom intensity value // will be in row, column order

how to convert an image from BGR to LAB with opencv 2.4 python 2.7 and numpy

I am working with opencv 2.4 and numpy. I would like to open an image and get all the information about it (8 bit - if its RGB-BGR etc) and also try to change the color space.
I have this code:
if __name__ == '__main__':
img = cv2.imread('imL.png')
conv= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
cv2.imwrite('lab.png', conv )
When I open the file lab.png I get the image with different colors!
I check the value of BGR to LAB in: http://www.brucelindbloom.com/
For this I would like to know all the information about one image.
That rigtht you will get a different image colour because imwrite() saves file in the format specified (PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order). images can be saved using this function. imwrite() doesn't know the format LAB to save image as it always expect the image in BGR.
If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving.
Lab is another color space, like the BGR color space which is gained from cv2.imread(). It just like you convert temperature from Fahrenheit to Celsius.
32 Fahrenheit and 0 Celsius is the same temperature but in different unit.
cv2.imwrite() dose not know if the values are in BGR color space or not. When it get a 3 dimension array, it assume that it is a BGR color space while your conv variable contains Lab color space. This is why your color of your image is changed.
For your information, Each layer of BGR color space contains blue, green and red colors while layers of Lab contains lightness (0-100), a* and b* respectively. For more information, please see "Lab color space" in Wikipedia.

Generate Color Histogram around a contoured object

Hey OpenCV/Emgu gurus,
I have an image that I am generating contour for, see below. I am trying to generate a color histogram based pruning of search space of images to look for. How can I get the mask around just the prominent object contour and block out the remaining. So I have a 2 part question:
How do I "invert" the image outside the contour? Floodfill invert, not? I am confused with all the options in OpenCV.
Second, how do I generate a 1-d color histogram from the contoured object in this case the red car to exclude the black background and only generate the color histogram that includes the car.
How would I do that in OpenCV (preferably in Emgu/C# code)?
Perhaps something like this? Done using the Python bindings, but easy to translate the methods to other bindings...
#!/usr/local/bin/python
import cv
import colorsys
# get orginal image
orig = cv.LoadImage('car.jpg')
# show orginal
cv.ShowImage("orig", orig)
# get mask image
maskimg = cv.LoadImage('carcontour.jpg')
# split original image into hue and value
hsv = cv.CreateImage(cv.GetSize(orig),8,3)
hue = cv.CreateImage(cv.GetSize(orig),8,1)
val = cv.CreateImage(cv.GetSize(orig),8,1)
cv.CvtColor(maskimg,hsv,cv.CV_BGR2HSV)
cv.Split(hsv, hue, None, val, None)
# build mask from val image, select values NOT black
mask = cv.CreateImage(cv.GetSize(orig),8,1)
cv.Threshold(val,mask,0,255,cv.CV_THRESH_BINARY)
# show the mask
cv.ShowImage("mask", mask)
# calculate colour (hue) histgram of only masked area
hue_bins = 180
hue_range = [0,180]
hist = cv.CreateHist([hue_bins], cv.CV_HIST_ARRAY, [hue_range], 1)
cv.CalcHist([hue],hist,0,mask)
# create the colour histogram
(_, max_value, _, _) = cv.GetMinMaxHistValue(hist)
histimg = cv.CreateImage((hue_bins*2, 200), 8, 3)
for h in range(hue_bins):
bin_val = cv.QueryHistValue_1D(hist,h)
norm_val = cv.Round((bin_val/max_value)*200)
rgb_val = colorsys.hsv_to_rgb(float(h)/180.0,1.0,1.0)
cv.Rectangle(histimg,(h*2,0),
((h+1)*2-1, norm_val),
cv.RGB(rgb_val[0]*255,rgb_val[1]*255,rgb_val[2]*255),
cv.CV_FILLED)
cv.ShowImage("hist",histimg)
# wait for key press
cv.WaitKey(-1)
This is a little bit clunky finding the mask - I wonder perhaps due to JPEG compression artefacts in the image... If you had the original contour it is easy enough to "render" this to a mask instead.
The example histogram rendering function is also a wee bit basic - but I think it shows the idea (and how the car is predominately red!). Note how OpenCV's interpretation of Hue ranges only from [0-180] degrees.
EDIT: if you want to use the mask to count colours in the original image - edit as so from line 15 downwards:
# split original image into hue
hsv = cv.CreateImage(cv.GetSize(orig),8,3)
hue = cv.CreateImage(cv.GetSize(orig),8,1)
cv.CvtColor(orig,hsv,cv.CV_BGR2HSV)
cv.Split(hsv, hue, None, None, None)
# split mask image into val
val = cv.CreateImage(cv.GetSize(orig),8,1)
cv.CvtColor(maskimg,hsv,cv.CV_BGR2HSV)
cv.Split(hsv, None, None, val, None)
(I think this is more what was intended, as the mask is then derived separately and applied to a completely different image. The histogram is roughly the same in both cases...)

Resources