i am working on bayer images. i use basler camera and i get bayerBG8 image from it. in pylon i can directly converted to RGB, but i want to save the bayer image so i need to specify its channels to define a mat file. when i use 1 channel for it i get gray scale image and if i choose more than on channel, images repeated in one frame as shown:
Bayer filter is a technique to use a single-channel image sensor to receive color image by defining which pixel should represent which color, so a bayer image should have one channel.
Related
I have a png image which is in grayscale 'test.png'. I need apply adaptive threshold to this image. I am using OpenCV.
image = cv2.imread('test_big.png')
im = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
I am not able to apply adaptive threshold since the image is not in grayscale.
So I tried to read the image as grayscale:
image = cv2.imread('test_big.png',1)
Now I can apply adaptive threshold but the output will be a blue and red image instead of black and white. Can anyone help?
The fault lies in the second code snippet:
image = cv2.imread('test_big.png',1)
Although you have said that test_big.png is a grayscale image, you have declared it as a color image (RGB) with three channels.
Hence you have to change the code to
image = cv2.imread('test_big.png', 0)
0 -> grayscale image
1 -> color image
You can also try:
cv2.imread('test_big.png', cv2.IMREAD_GRAYSCALE)
The bottom line is: although the image being read is a grayscale image, the system will not recognize it until it is explicitly specified. In your case, your image was a grayscale image, but since you declared it as a color image it considered the image to have three channels (RGB) and hence the subsequent adaptive threshold function did not execute.
I'm reading an image and doing some processing on the blue channel without changing the Red nor the green channels.
When i finished processing the blue channel, i merged back the three channels into one RGB image. and when i use imshow to view the channels, every thing is alright and i can see that the changes i've made only affect the Blue channel and they do not affect the red nor the green ones.
Up to this point every thing is alright !
But when i save the image using imwrite, the resulting image is slightly different, in that the changes made on the blue channel seem to get propagated to the red and green channels, it's like imwrite is doing some kind of mean between the 3 channels :
image = imread('image.jpg', IMREAD_COLOR);
split(image, channels);
// Create some changes on channels[0]
merge(channels, 3, image);
// Up to this point every thing is alright
imwrite("modified.jpg", image); // Image changes when written;
Is there any solution to avoid this behavior ?
JPG is a lossy format: https://en.wikipedia.org/wiki/JPEG
JPEG (/ˈdʒeɪpɛɡ/ JAY-peg)1 is a commonly used method of lossy
compression for digital images, particularly for those images produced
by digital photography. The degree of compression can be adjusted,
allowing a selectable tradeoff between storage size and image quality.
JPEG typically achieves 10:1 compression with little perceptible loss
in image quality.
Solution: Use a lossles Format like PNG to save your image.
I need to be able to work with images where some regions are grayscale while others are kept on the RGB format. I don't want to convert an image into a grayscale since it will lose the channels and will become simply one channeled, is there a way to keep the RGB channels of some pixels on the picture and turn the others into a grayscale?
NO.
I see two solutions to this:
Have both a gray (Mat1b) and a rgb (Mat3b) image, and work on the image you need.
Have a single rgb (Mat3b) image, and set r,g,b channels to the same gray value where you need. In this way you can mimic to have a mixed gray/rgb image.
In open cv to remove background, using current frame and former frame, i applied absdiff function and created a difference image in gray scale. However, i would like to covert the gray scale image back in to RGB with actual color of the image, but i have no idea how to operate this back in.
I'm using C++.
Could any one knowledgeable of open cv help me?
You cannot covert the gray scale image back into RGB with actual color of the image again as coverting RGB to gray scale is a data-losing process.
Instead, as #MatsPetersson suggested, you can take the use of the grayscale image to create a mask, e.g. by further applying a thresholding process. Then you can easily get the ROI color image by:
cv::Mat dst;
src.copyTo(dst, mask);
i have converted an image from RGB to B/W then i want to convert it back to RGB but i have a problem on that:
my code:
int width=zoomedImage->width;
int height=zoomedImage->height;
TempImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvCvtColor(zoomedImage, TempImage,CV_RGB2GRAY);
cvThreshold( TempImage, TempImage,128,256,CV_THRESH_BINARY);
cvCvtColor( TempImage,zoomedImage,CV_GRAY2RGB);
this->pictureBox1->Image=(gcnew
System::Drawing::Bitmap(zoomedImage->width,zoomedImage->height,zoomedImage->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)zoomedImage->imageData));
here i'm displaying zoomedImage as a B/W image,in another action i want to display zoomedImage as an RGB image the major problem here is that i can't change the image that will be draw as another parts of my code is depending on this sequence, i wrote that in the other action:
cvCvtColor( TempImage,zoomedImage,CV_GRAY2RGB);
this->pictureBox1->Image=(gcnew
System::Drawing::Bitmap(zoomedImage->width,zoomedImage->height,zoomedImage->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)zoomedImage->imageData));
but zoomedImage still dispalyed as B/W, i heared that when a true color image is converted to gray it can't be returned again as a true color image, so what does CV_GRAY2RGB do?
When you convert an RGB image to a gray level image, color information is lost, and this information cannot be recovered fom the gray level image again.
When you try to convert B/W image to RGB you only make a 3 channel image, but all channels contain the same intensity data. Hence you get a gray level image with 3 channels. Nothing more.
i have solved my problem as following:
Convert Original image to B/W
int width=zoomedImage->width;
int height=zoomedImage->height;
ColorSaver=cvCreateImage(cvSize(width,height),zoomedImage->depth,zoomedImage->nChannels);
ColorSaver=cvCloneImage(zoomedImage);
TempImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvCvtColor(zoomedImage, TempImage,CV_RGB2GRAY);
cvThreshold( TempImage, TempImage,128,256,CV_THRESH_BINARY);
cvCvtColor( TempImage,zoomedImage,CV_GRAY2RGB);
this->pictureBox1->Image=(gcnew
System::Drawing::Bitmap(zoomedImage->width,zoomedImage->height,zoomedImage->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)zoomedImage->imageData));
return back the original image to RGB:
zoomedImage=cvCloneImage(ColorSaver);
this->pictureBox1->Image=(gcnew
System::Drawing::Bitmap( zoomedImage->width, zoomedImage->height, zoomedImage->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) zoomedImage->imageData));