Applying adaptive threshold to a grayscale image - python-2.7

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.

Related

how many channels does a bayer image have?

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.

image export is returning grey scale image in opencv

I am using Network Optix Video management service. Using their application I am building a plugin. For my purpose I want to export frame as an image from a video. for that I used following code to convert to cv object and saving into my file.
cv::Mat img_color;
cv::Mat img(
videoFrame->height(),/*_rows*/
videoFrame->width(), /*_cols*/
CV_8UC1, //< BGR color space (default for OpenCV) /*_type*/
(void*) videoFrame->data(0), /*_data*/
(size_t) videoFrame->lineSize(0)); /*_step*/
cv::cvtColor(img, img_color, CV_GRAY2RGB);
m_lastVideoFrameTimestampUs = videoFrame->timestampUs();
std::string file_path = "/var/www/html/images/"+std::to_string(m_lastVideoFrameTimestampUs)+".jpg";
cv::imwrite(file_path,img_color);
below screenshot is what I am getting on Network Optix client application.
But, this is what I am getting as an image file on my machine.
cvtColor doesn't have any effect on the image
I think, CV_8UC1 argument should be modified so that I will get RGB image
EDIT - 1:
changes CV_8UC1 to CV_8UC3
Result turned into 3 segments of image
CV_8UC1 means that it is 8-bit single-channel array, you are getting a grayscale image at first and you can not except cvtColor to get it colorized again. cvtColor which you used will convert the image to BGR but all the channels will be in same value so it will continue to seem as grayscale.
In this case you can use CV_8UC3 which means that it is an 8-bit unsigned integer matrix/image with 3 channels(If your image in 3 channels)

how to read only one channel of image color from disk in opencv with C++

I need to read large numbers of images with high speed requirements, and just need to handle the Blue channel of a color image.
If I read image with cv::imread(imgName, CV_LOAD_IMAGE_COLOR); It will be very long time, so I want to read only one color image channel. How to do it ??? thanks very much !!
OpenCV doesn't provide any method to load only a specific channel. However, you have a few options.
Load as color image and extract the channel you need
cv::Mat3b img("path/to/image", cv::IMREAD_COLOR);
cv::Mat1b blue;
cv::extractChannel(img, blue, 0);
This is a little faster than using the split approach, but you still need to load the color image.
In a preprocessing stage, load all your images (you can use glob to retrieve all images into a folder), extract the blue channel and store it as grayscale. Then you can load the image as grayscale.
// Preprocessing
cv::String folder = "your_folder_with_images/*.jpg";
std::vector<cv::String> filenames;
cv::glob(folder, filenames);
for (size_t i = 0; i < std::filenames.size(); ++i)
{
cv::Mat3b img = cv::imread(filenames[i], cv::IMREAD_COLOR);
cv::Mat1b blue;
cv::extractChannel(img, blue, 0);
cv::imwrite("some/other/name", blue);
}
// Processing
cv::Mat1b blue = imread("path/to/image", cv::IMREAD_UNCHANGED);
You can improve speed by saving / loading the image in binary format:
// Preprocessing
...
matwrite("some/other/name", blue);
// Processing
cv::Mat1b blue = matread("path/to/image");
I think you can not do this, at least with OpenCV. If you check the documentation of cv::imread you will see that there is no option to read only one color channel:
IMREAD_UNCHANGED: If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE: If set, always convert image to the single channel grayscale image.
IMREAD_COLOR: If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH: If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR: If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL: If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2: If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2: If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4: If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4: If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8: If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8: If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION: If set, do not rotate the image according to EXIF's orientation flag.
If you want, you can split the channels of a matrix after loading it usin Mat::split:
Mat src = imread("img.png",CV_LOAD_IMAGE_COLOR); //load image
Mat bgr[3]; //destination array
split(src,bgr);//split source
//Note: OpenCV uses BGR color order
imwrite("blue.png",bgr[0]); //blue channel
imwrite("green.png",bgr[1]); //green channel
imwrite("red.png",bgr[2]); //red channel

In open cv, how can i convert gray scale image back in to RGB image(color)

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);

OpenCV convert from B/W to RGB

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));