OpenCV Pixel with "no value" - opencv

Is it possible in OpenCV to have pixels with no colour? Like a transparent layer or in gimp if you delete all colours or elements on a layer.

Yes you can using BGRA system by making the alpha value equal to 0.
Example:
cv::Mat img(ROW, COLS, CV_8UC4, cv::Scalar(B_VALUE,G_VALUE,R_VALUE,0 /* This is the alpha*/));
Now all the this cv::Mat pixels are 100% transparent. You can change the alpha value to 255 so it is fully opaque or any value in the range [0,255] for a degree of transparent.

Related

Comparing histograms without white color included OpenCV

Is there a way that compares histograms but for example white color to be excluded and so white color doesn't affect onto the comparison.
White pixels have Saturation, S = 0. So, it is very easy to remove the white pixels from being counted while creating histogram. Do the following:
Convert your image from BGR to HSV
Then split your HSV image into three individual channels i.e. H, S and V
Then, access each pixel of channel S and if the pixel value = 0 (means S = 0) then it mean that it is a white pixel.
If the pixel is white then do not consider its Hue value to create histogram and if not...then put its hue value into the corresponding bin (normal procedure to build histogram).
Summary: you just need to find white pixels by checking their Saturation value, which is S = 0.
PS: Have a look at this link to understand the HSV model.

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

Know a pixel is transparent

I know how to get each pixel from a image and its RGBA value. My question is how to determine a pixel is transparent or not. Please help
Check if alpha part of rgba value is equal to 0, if it is that means the pixel is fully transparent. ANd if it's equal to 255 isn't transparent at all.

opencv: how to change the transparency level of an image

Is it possible to change the transparency level of an IplImage (the alhpa channel)? I can do this using cvSet and setting all the values to a cvScalar, but that would change not only the alpha channel, but the actual RGB channels, as well.
thanks for the help.
You can add a scalar to an RGBA image with cvAddS:
void cvAddS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL)
For increasing just the alpha channel of an image you could use for example a cvScalar(0,0,0,30).
This will increase the alpha value by 30.
The same way cvSubS can be applied to substract a scalar.

how to set hue value of some pixel with opencv

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°).

Resources