I am trying to read a .tif or .tiff floating point gray scale image in OpenCV.
I can read and write routine file format such as png, jpg etc but I am not able to read from my Desktop a format I never used before which is .tif or .tiff format.
The image: the image I am trying to read has the following parameters:
Size:
And width and height:
After some documentation and various sources I was able to understand that it is possible to use a convertTo function to convert between available data types, the source can be found here. However this didn't work well and I actually had a compilation error saying:
OpenCV(3.4.1) Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/to/opencv/modules/highgui/src/window.cpp, line 356
terminate called after throwing an instance of cv::Exception
what(): OpenCV(3.4.1) /home/to/opencv/modules/highgui/src/window.cpp:356: error: (-215) size.width>0 && size.height>0 in function imshow
The code I am using is the following:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat img = imread("/home/to/Desktop/example.tif");
cv::imshow("source",img);
Mat dst; // destination image
// check if we have RGB or grayscale image
if (img.channels() == 3) {
// convert 3-channel (RGB) 8-bit uchar image to 32 bit float
img.convertTo(dst, CV_32FC3);
}
else if (img.channels() == 1) {
// convert 1-chanel (grayscale) 8-bit uchar image to 32 bit float
img.convertTo(dst, CV_32FC1);
}
// display output, note that to display dst image correctly
// we have to divide each element of dst by 255 to keep
// the pixel values in the range [0,1].
cv::imshow("output",dst/255);
waitKey();
}
Additional example I tried to make it work is directly from the OpenCV documentation which can be found here, with a small modification though. I read from official documentation that the options IMREAD_ANYCOLOR | IMREAD_ANYDEPTH should also be activated and in fact is what I did in the second additional trial below:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
String imageName( "/home/to/Desktop/example.tif" ); // by default
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
Mat outImage;
image = imread( imageName, IMREAD_ANYCOLOR | IMREAD_ANYDEPTH ); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
resize(image, outImage, cv::Size(500,500));
imshow("orig", image);
imshow("resized", outImage);
// Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
This time the compiler runs without any error but no image is shown as it is possible to see from the print screen below:
UPDATE
This is the result after the cv::resize
UPDATE 2
This the result after applying imshow("Display window", image*10);
Is there something that I am missing from the official documentation or something else I am forgetting to do?
Thanks for shedding light on this issue.
Your image is composed of a single channel of 64-bit floats which range from -219.774 to -22.907. I can tell that using tiffutil which is shipped with libtiff:
tiffutil -verboseinfo image.tif
TIFFReadDirectory: Warning, Unknown field with tag 33550 (0x830e) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33922 (0x8482) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 42113 (0xa481) encountered.
Directory at 0x256b3a2
Image Width: 2277 Image Length: 2153
Bits/Sample: 64
Sample Format: IEEE floating point
Compression Scheme: none
Photometric Interpretation: "min-is-black"
Samples/Pixel: 1
Rows/Strip: 1
Number of Strips: 2153
Strips (Offset, ByteCount):
17466, 18216
35682, 18216
53898, 18216
...
...
I am not certain exactly what you plan to do, but as a first stab, you can just add 220 to every pixel and convert to unsigned char and your range will be 0 to 197 which is perfectly displayable:
I actually did it using Python because I am quicker with that, but the C++ will follow exactly the same format:
import cv2
# Load image
img = cv2.imread('image.tif',cv2.IMREAD_UNCHANGED)
# Add 220 to all values, round to unsigned 8-bit and display
Image.fromarray((img+220).astype(np.uint8)).show()
Related
I am trying to convert color images to gray-scale using OpenCV 2.4.11 C++ for Visual Studio 2012. I have used the following code to convert the image to grayscale. However, I am unable to do so because I am not able to read the image.
The message I get is "Error reading image" because img is empty. I have stored the required image in the Debug folder beside the exe file. I have also mentioned the image name as a command argument in the Debug section of the property pages. I am also trying to store the grayscale image in the disk. Thanks in advance. The code is as follows:
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
using namespace cv;
int main(int argc, const char**argv)
{
Mat img=imread("Mountain_8-Bit_Grayscale.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if(img.empty())
{
std::cout<< " --(!) Error reading image " << std::endl;
system("pause");
return -2;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
imwrite("image_bw.jpg", img);
waitKey(0);
destroyWindow("MyWindow");
return 0;
}
You should try using the absolute path to the image not the relative path. The other steps are fine, the image is read properly and the image displaying and saving commands are given properly there is a path problem.
Thought I'd try my hand at a little (auto)correlation/convolution today in openCV and make my own 2D filter kernel.
Following openCV's 2D Filter Tutorial I discovered that making your own kernels for openCV's Filter2D might not be that hard. However I'm getting unhandled exceptions when I try to use one.
Code with comments relating to the issue here:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
//Loading the source image
Mat src;
src = imread( "1.png" );
//Output image of the same size and the same number of channels as src.
Mat dst;
//Mat dst = src.clone(); //didn't help...
//desired depth of the destination image
//negative so dst will be the same as src.depth()
int ddepth = -1;
//the convolution kernel, a single-channel floating point matrix:
Mat kernel = imread( "kernel.png" );
kernel.convertTo(kernel, CV_32F); //<<not working
//normalize(kernel, kernel, 1.0, 0.0, 4, -1, noArray()); //doesn't help
//cout << kernel.size() << endl; // ... gives 11, 11
//however, the example from tutorial that does work:
//kernel = Mat::ones( 11, 11, CV_32F )/ (float)(11*11);
//default value (-1,-1) here means that the anchor is at the kernel center.
Point anchor = Point(-1,-1);
//value added to the filtered pixels before storing them in dst.
double delta = 0;
//alright, let's do this...
filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
imshow("Source", src); //<< unhandled exception here
imshow("Kernel", kernel);
imshow("Destination", dst);
waitKey(1000000);
return 0;
}
As you can see, using the tutorials kernel works fine, but my image will crash the program, I've tried changing the bit-depth, normalizing, checking size and lots of commenting out blocks to see where it fails, but haven't cracked it yet.
The image is, '1.png':
And the kernel I want 'kernel.png':
I'm trying to see if I can get a hotspot in dst at the point where the eye catchlight is (the kernel I've chosen is the catchlight). I know there are other ways to do this, but I'm interested to see how effective convolving the catchlight over itself is. (autocorrelation I think that's called?)
Direct questions:
why the crash?
is the crash indicating a fundamental conceptual mistake?
or (hopefully) is it just some (silly) fault in the code?
Thanks in advance for any help :)
The assertion error should be posted which would help someone to answer you other than questioning why is the crash. Anyways, I have posted below the possible errors and solution for convolution filter2D.
Error 1:
OpenCV Error: Assertion failed (src.channels() == 1 && func != 0) in cv::countNo
nZero, file C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\core\src\st
at.cpp, line 549
Solution : Your input Image and the kernel should be grayscales. You can use the flag 0 in imread. (ex. cv::imread("kernel.png",0) to read the image as grayscale.) If you want to apply different kernels to different channels, split the image into separate color planes using split() and process them individually.
I don't see anything other than the obove error that may crash. Kernel size should in odd numbers and your kernel image is 11X11 which is fine. If it stills crashes kindly provide more information in order to help you out.
I'm pretty new to OpenCV, so bear with me. I'm running a Mac Mini with OSX 10.8. I have a program that recognizes colors and displays them in binary picture (black and white). However, I want to store the number of white pixels as an integer (or float, etc.) to compare with other number of pixels. How can I do this? Here is my current code-
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); //capture the video from webcam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
namedWindow("HSVLeftRed", CV_WINDOW_AUTOSIZE);
namedWindow("HSVLeftGreen", CV_WINDOW_AUTOSIZE);
while (true) {
Mat image;
cap.read(image);
Mat HSV;
Mat leftgreen;
Mat leftred;
//Left Cropping
Mat leftimg = image(Rect(0, 0, 640, 720));
//Left Red Detection
cvtColor(leftimg,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(0,0,150),Scalar(0,0,255), leftgreen);
//imshow("HSVLeftRed", leftgreen);
//print pixel type
//Left Green Detection
cvtColor(leftimg,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(still need to find proper min values),Scalar(still need to find proper max values), leftgreen);
//imshow("HSVLeftGreen", leftgreen);
//compare pixel types
}
return 0;
}
Thanks in advance!
To count the non-zero pixels, OpenCV has this function cv::countNonZero. It takes input the image, whose number of non-zero pixels, we want to calculate and output is number of non-zero pixels(int). Here is the documentation.
In your case, since all the pixels are either black or white, all the non zero pixels will be white pixels.
This is how to use it,
int cal = countNonZero(image);
Change image, as per your code.
I am using OpenCV 2.4.4 on a Cent OS machine. My code currently loads an image with the warning: component data type mismatch
here is the code:
#include <cv.h>
#include <highgui.h>
#include "imglib.h"
int main( int argc, char** argv )
{
Mat image = imread( argv[1], CV_LOAD_IMAGE_ANYDEPTH);
imwrite("debugwriteout.jp2", image);
}
I pass the name of a .jp2 greyscale file in the args. The image has a 14-bit pixel depth, but when I print out the pixel values I get values over 20000, and my image is now a completely black square. Any advice would be appreciated.
Additional information:
When I change the imread flag to CV_LOAD_IMAGE_GRAYSCALE it successfully convert the image to an 8-bit pixel depth and prints useful output so I can tell that the jasper module is working at least somewhat correctly.
Any advice would be appreciated,
Thanks
SZman,
I solved my problem.
The solution is the position of the high bit.
On 16 bits, for a 14 bits depth, you have xxxxxxxxxxxxxx00 instead of 00xxxxxxxxxxxxxx.
If you want the correct value, you must decal of 2 bits on the right.
Please read the image using those flags
Mat image = imread( argv[1], CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
I am trying to watermark an image into a video sequence. The process requires decomposition of frames into SVD which I am trying to achieve using the partial code below. The SVD constructor at line 47 fails with a segmentation fault.
gdb reports the following error:
"Program received signal SIGSEGV, Segmentation fault.
0xb5d31ada in dlange_ () from /usr/lib/liblapack.so.3gf"
#include <iostream>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
const unsigned int MAX = 10000;
using namespace cv;
using namespace std;
int NO_FRAMES;
bool check_exit()
{
return (waitKey(27) > 0)?true:false;
}
int main(int argc, char ** argv)
{
Mat rgb[MAX];
Mat ycbcr[MAX];
Mat wm_rgb[MAX];
namedWindow("watermark",1);
namedWindow("RGB", 1);
namedWindow("YCBCR",1);
VideoCapture capture(argv[1]);
Mat watermark = imread(argv[2]);
int i=0;
capture >> rgb[i];
imshow("watermark", watermark);
while(!rgb[i].empty())
{
imshow("RGB", rgb[i]);
cvtColor(rgb[i], ycbcr[i], CV_RGB2YCrCb);
imshow("YCBCR", ycbcr[i]);
i++;
capture >> rgb[i];
cout<<"frame "<<i<<endl;
if(check_exit())
exit(0);
}
//This line creates Segmentation fault
SVD temp(rgb[0]);
capture.release();
return 0;
}
Being more familiar with the C interface, I'll just describe a few things that seem out of place in your C++ code:
The SVD() function expects the input image to be a floating point image, so you may need to convert scale to 32-bit from the standard 8-bit. Here's some very basic (and not very efficient) code for illustration purposes:
int N = img->width;
IplImage* W = cvCreateImage( cvSize(N, 1), IPL_DEPTH_32F , 1 );
IplImage* A = cvCreateImage( cvGetSize(img), IPL_DEPTH_32F , 1 );
cvConvertScale(img, A);
IplImage* W_mod = cvCreateImage( cvSize(N-l, 1), IPL_DEPTH_32F , 1 );
cvSVD(A, W, NULL, NULL, CV_SVD_MODIFY_A);
The SVD values are stored in the Nx1 matrix (IplImage in this case) named W. The input image img is converted to 32-bit in A. We used the CV_SVD_MODIFY_A flag to make it faster and modify values in A. The other options were left blank (NULL), but you can supply parameters as needed. Please check the OpenCV docs for those.
Hopefully you'll be able to figure out from this working code what was wrong in your C++ code.