I am using OpenCV and saving as a jpeg using the cvSaveImage function, but I am unable to find the Jpeg compression factor used by this.
What's cvSaveImage(...)'s Jpeg Compression factor
How can I pass the compression factor when using cvSaveImage(...)
Currently cvSaveImage() is declared to take only two parameters:
int cvSaveImage( const char* filename, const CvArr* image );
However, the "latest tested snapshot" has:
#define CV_IMWRITE_JPEG_QUALITY 1
#define CV_IMWRITE_PNG_COMPRESSION 16
#define CV_IMWRITE_PXM_BINARY 32
/* save image to file */
CVAPI(int) cvSaveImage( const char* filename, const CvArr* image,
const int* params CV_DEFAULT(0) );
I've been unable to find any documentation, but my impression from poking through this code is that you would build an array of int values to pass in the third parameter:
int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = desired_quality_value;
p[2] = 0;
I don't know how the quality value is encoded, and I've never tried this, so caveat emptor.
Edit:
Being a bit curious about this, I downloaded and built the latest trunk version of OpenCV, and was able to confirm the above via this bit of throwaway code:
#include "cv.h"
#include "highgui.h"
int main(int argc, char **argv)
{
int p[3];
IplImage *img = cvLoadImage("test.jpg");
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 10;
p[2] = 0;
cvSaveImage("out1.jpg", img, p);
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 100;
p[2] = 0;
cvSaveImage("out2.jpg", img, p);
exit(0);
}
My "test.jpg" was 2,054 KB, the created "out1.jpg" was 182 KB and "out2.jpg" was 4,009 KB.
Looks like you should be in good shape assuming you can use the latest code available from the Subversion repository.
BTW, the range for the quality parameter is 0-100, default is 95.
OpenCV now has a parameter to set jpeg quality. I'm not sure exactly when this was introduced, but presumably sometime after 2.0.
const int JPEG_QUALITY = 80;
Mat src;
// put data in src
vector<int> params;
params.push_back(CV_IMWRITE_JPEG_QUALITY);
params.push_back(JPEG_QUALITY);
imwrite("filename.jpg", src, params);
If you are using C++0x, you can use this shorter notation:
imwrite("filename.jpg", src, vector<int>({CV_IMWRITE_JPEG_QUALITY, JPEG_QUALITY});
You can probably find this by poking around in the source code here: http://opencvlibrary.svn.sourceforge.net/viewvc/opencvlibrary/
You can't, as the function does not accept such a parameter. If you want to control the compression then the simplest method I can think of is first saving your image as a bitmap with cvSaveImage() (or another lossless format of your choice) and then use another image library to convert it to a JPEG of the desired compression factor.
imwrite("filename.jpeg",src,(vector<int>){CV_IMWRITE_JPEG_QUALITY, 20});
filename.jpeg will be output File name
src be source image read containing variable
(vector<int>) typecasting
{CV_IMWRITE_JPEG_QUALITY, 20} an array of elements to be passed as Param_ID - and Param_value in imwrite function
Related
I'm trying to pass a huge Mat image (98304x51968) between openCV and itk using the ITK to openCV Bridge. I have an error :
Insufficient memory (OverFlow for imageSize) in cvIniyImageHeader,
file opencv\modules\core\src\array.cpp line 2961.
Does this mean that opencv has a limit on the size of images?
Good news, since this pull request: handle huge matrices correctly #11505, you should be able to do something like this (code taken from the test):
Mat m(65000, 40000, CV_8U);
ASSERT_FALSE(m.isContinuous());
uint64 i, n = (uint64)m.rows*m.cols;
for( i = 0; i < n; i++ )
m.data[i] = (uchar)(i & 255);
cv::threshold(m, m, 127, 255, cv::THRESH_BINARY);
int nz = cv::countNonZero(m); // FIXIT 'int' is not enough here (overflow is possible with other inputs)
ASSERT_EQ((uint64)nz, n / 2);
Since countNonZero() returns an int, overflow is possible. This means that you should be able to create huge matrix but not all OpenCV function can handle correctly huge matrix.
Regarding your issue, this is the code for ITKImageToCVMat in v5.0a02:
template<typename TInputImageType>
cv::Mat
OpenCVImageBridge::ITKImageToCVMat(const TInputImageType* in, bool force3Channels)
{
// Extra copy, but necessary to prevent memory leaks
IplImage* temp = ITKImageToIplImage<TInputImageType>(in, force3Channels);
cv::Mat out = cv::cvarrToMat( temp, true );
cvReleaseImage(&temp);
return out;
}
As you can see, IplImage image is still used and should be the source of your error.
Your best option currently should be to do the conversion yourself. Maybe something like (I don't know ITK, same input and output type, one channel):
typename ImageType::RegionType region = in->GetLargestPossibleRegion();
typename ImageType::SizeType size = region.GetSize();
unsigned int w = static_cast< unsigned int >( size[0] );
unsigned int h = static_cast< unsigned int >( size[1] );
Mat m(h, w, CV_8UC1, in->GetBufferPointer());
No copy is involved here. If you want to copy, you can do:
Mat m_copy = m.clone();
There seems to be a signed int (typically 32 bit) limitation in IplImage:
From the named .cpp file here's the code snippet that leads to the error:
const int64 imageSize_tmp = (int64)image->widthStep*(int64)image->height;
image->imageSize = (int)imageSize_tmp;
if( (int64)image->imageSize != imageSize_tmp )
CV_Error( CV_StsNoMem, "Overflow for imageSize" );
Which looks like (without checking) image->imageSize is a 32 bit signed int and this part of the code will detect and handle overflows. According to your posted link in the comments, the IplImage "bug" might got fixed (I didn't check that), so MAYBE you can remove this overflow detection step in the OpenCV code for newer IplImage versions, but that's just a guess and has to be confirmed. You'll have to check the type of image->imageSize. If it is a 64 bit type, you can probably change the openCV code to support Mats bigger than 2147483647 bytes.
EDIT: REMARK: I checked the code in OpenCV 3.4 but the code line was the right one, so probably in Version 4.0 there's no change yet.
If your are sure that the IplImage limitation got fixed, you can try this:
const int64 imageSize_tmp = (int64)image->widthStep*(int64)image->height;
image->imageSize = imageSize_tmp; // imageSize isn't 32 bit signed int anymore!
//if( (int64)image->imageSize != imageSize_tmp ) // no overflow detection necessary anymore
// CV_Error( CV_StsNoMem, "Overflow for imageSize" ); // no overflow detection necessary anymore
but better make sure that IplImage's imageSize is 64 bit now ;)
UPDATE: The linked fix in https://github.com/opencv/opencv/pull/7507/commits/a89aa8c90a625c78e40f4288d145996d9cda3599 ADDED the overflow detection, so PROBABLY IplImage still has the 32 bit int imageSize limitation! Be careful here!
I want to load an image in c++ opencv that comes from a postgresql database.
The image, jpg extension, is stored as a binary data (bytea type) in the base, that I can access thanks to libpqxx.
The problem is that I do not know how to convert the data into a cv::Mat instance. With a regular image I could use imread('myImage.jpg', ...), but in this case I cannot even load the database image in the data attribute of Mat because it is jpeg and not bmp.
Any idea ? Is there some opencv method I could use that could understand directly the binary data and convert it to the appropriate structure ? the imdecode() functions seems to be used for bitmap datas.
edit : Berak, using a vector the imdecode function returns null Matrice What happens "If the buffer is too short or contains invalid data, the empty matrix/image is returned." Here is the code :
pqxx::result r=bdd::requete("SELECT image FROM lrad.img WHERE id=3",1);//returns the bytea image in r[0]["image"]
const char* buffer=r[0]["image"].c_str();
vector<uchar>::size_type size = strlen((const char*)buffer);
vector<uchar> jpgbytes(buffer, buffer+size);
Mat img = imdecode(jpgbytes, CV_LOAD_IMAGE_COLOR);
//jpgbytes.size()=1416562 img.size()=[0 x 0]
What am I missing ?
still, use imdecode . it can handle png,jpg,bmp,ppm,webp,jp2,exr, but no gif.
vector<uchar> jpgbytes; // from your db
Mat img = imdecode(jpgbytes);
(you should do the same for bmp or any other supported formats, don't mess with Mat's raw data pointers!)
Ok I have the process to convert a bytea data to a cv::Mat, here is the code.
inline int dec(uchar x){ //convert uchar to int
if (x>='0'&&x<='9') return (x-'0');
else if (x>='a'&&x<='f') return (x-'a'+10);
else if (x>='A'&&x<='F') return (x-'A'+10);
return 0;
}
cv::Mat bytea2Mat(const pqxx::result::field& f){
const char* buffer=f.c_str();
vector<uchar>::size_type size = strlen((const char*)buffer);
vector<uchar> jpgbytes(size/2-1);
for (size_t i=0; i!=size/2-1;i++) {
jpgbytes[i]=(dec(buffer[2*(i+1)])<<4)+dec(buffer[2*(i+1)+1]);
}
cout <<size/2<<";"<<jpgbytes.size()<<endl;
return imdecode(jpgbytes, CV_LOAD_IMAGE_COLOR);
}
The bytea output is encrypted as a char* looking like "\x41204230", for an original input string of "a b0" in hexa form. (the \x may not be present according to the data input)
to get the original data you have to calculate the original input from the two char, ('4','1'= 0x41=65). The vector is half the size of the char*.
I am trying to learn some basics, but I keep getting errors.
I am trying to resize a cv::Mat
Edit to clarify why I am not loading an image: I am trying to test without having access to external files.
Inside the code, though, I put int x = M.depth(); and it read 0
cv::Mat M(2,2, CV_8UC3, cv::Scalar(0,0,255));
scale = 2;
cv::Size myImageSize;
myImageSize.height = M.rows;
myImageSize.width = M.cols;
cv::Mat ImgCopy = cvCreateImage(myImageSize, M.depth(), M.channels());
..
cv::resize(M, ImgCopy, myImageSize, 0, 0, CV_INTER_LINEAR);
I am getting an error on the line cvCreateImage
Input image depth is not supported by function (Unsupported format) in unknown functio, file... \modules\core\src\array.cpp...
I have tried with other values (CV_32F...) and I get the same error.
Please help !
cvCreateImage() is for use with the C API and returns an IplImage*. To initialize a cv::Mat, use the appropriate constructor.
cv::Mat ImgCopy(M.size(), M.type());
is the most succinct way to create and allocate memory for a new cv::Mat.
However, cv::resize() makes it even easier. You just have to declare your image
cv::Mat ImgCopy
and the arguments to cv::resize() allow automatic calculation of the correct dimensions and type. The last three parameters do not need to be specified because you don't change them from the defaults.
cv::resize(M, ImgCopy, myImageSize);
Try this code :
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
IplImage *src,*dst;
src=cvLoadImage("img.png");
cvNamedWindow("SRC", CV_WINDOW_AUTOSIZE);
cvNamedWindow("DST", CV_WINDOW_AUTOSIZE);
dst = cvCreateImage(cvSize(500,500),src->depth,src->nChannels);
cvResize(src,dst);
cvShowImage("SRC",src);
cvShowImage("DST",dst);
cvWaitKey(0);
cvDestroyWindow("SRC");
cvDestroyWindow("DST");
cvReleaseImage(&dst);
cvReleaseImage(&src);
return 0;
}
Given the following
Bitmap raw image data in char array
Image width and height
Path wzAppDataDirectory in std::wstring generated using the following code
// Get a good path.
wchar_t wzAppDataDirectory[MAX_PATH];
wcscpy_s( wzAppDataDirectory, MAX_PATH, Windows::Storage::ApplicationData::Current->LocalFolder->Path->Data() );
wcscat_s( wzAppDataDirectory, MAX_PATH, (std::wstring(L"\\") + fileName).c_str() );
How can we save the image as JPG? (Include encoding as well as the char array is raw bitmap form)
Code example is very much appreciated.
You'll need to use a library to encode the JPEG. Some possibilities are the Independent JPEG Group's jpeglib, stb_image, or DevIL.
This is an example code which I obtained from my friend.
It uses OpenCV's Mat data structure. Note that, you need to ensure the unsigned char data array within cv::Mat is in continuous form. cv::cvtColor will do the trick (Or, cv::Mat.clone).
Take note, do not use OpenCV's imwrite. As at current time of writing, imwrite doesn't pass Windows Store Certification Test. It is using several APIs, which is prohibited in WinRT.
void SaveMatAsJPG(const cv::Mat& mat, const std::wstring fileName)
{
cv::Mat tempMat;
cv::cvtColor(mat, tempMat, CV_BGR2BGRA);
Platform::String^ pathName = ref new Platform::String(fileName.c_str());
task<StorageFile^>(ApplicationData::Current->LocalFolder->CreateFileAsync(pathName, CreationCollisionOption::ReplaceExisting)).
then([=](StorageFile^ file)
{
return file->OpenAsync(FileAccessMode::ReadWrite);
}).
then([=](IRandomAccessStream^ stream)
{
return BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream);
}).
then([=](BitmapEncoder^ encoder)
{
const Platform::Array<unsigned char>^ pixels = ref new Platform::Array<unsigned char>(tempMat.data, tempMat.total() * tempMat.channels());
encoder->SetPixelData(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Ignore, tempMat.cols , tempMat.rows, 96.0, 96.0, pixels);
encoder->FlushAsync();
});
}
So I'm trying to use the webp API to encode images. Right now I'm going to be using openCV to open and manipulate the images, then I want to save them off as webp. Here's the source I'm using:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <webp/encode.h>
int main(int argc, char *argv[])
{
IplImage* img = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;
if (argc<2) {
printf("Usage:main <image-file-name>\n\7");
exit(0);
}
// load an image
img=cvLoadImage(argv[1]);
if(!img){
printf("could not load image file: %s\n",argv[1]);
exit(0);
}
// get the image data
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
printf("processing a %dx%d image with %d channels \n", width, height, channels);
// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin",100,100);
// invert the image
for (i=0;i<height;i++) {
for (j=0;j<width;j++) {
for (k=0;k<channels;k++) {
data[i*step+j*channels+k] = 255-data[i*step+j*channels+k];
}
}
}
// show the image
cvShowImage("mainWin", img);
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img);
float qualityFactor = .9;
uint8_t** output;
FILE *opFile;
size_t datasize;
printf("encoding image\n");
datasize = WebPEncodeRGB((uint8_t*)data,width,height,step,qualityFactor,output);
printf("writing file out\n");
opFile=fopen("output.webp","w");
fwrite(output,1,(int)datasize,opFile);
}
When I execute this, I get this:
nato#ubuntu:~/webp/webp_test$ ./helloWorld ~/Pictures/mars_sunrise.jpg
processing a 2486x1914 image with 3 channels
encoding image
Segmentation fault
It displays the image just fine, but segfaults on the encoding. My initial guess was that it's because I'm releasing the img before I try to write out the data, but it doesn't seem to matter whether I release it before or after I try the encoding. Is there something else I'm missing that might cause this problem? Do I have to make a copy of the image data or something?
The WebP api docs are... sparse. Here's what the README says about WebPEncodeRGB:
The main encoding functions are available in the header src/webp/encode.h
The ready-to-use ones are:
size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height,
int stride, float quality_factor, uint8_t** output);
The docs specifically do not say what the 'stride' is, but I'm assuming that it's the same as the 'step' from opencv. Is that reasonable?
Thanks in advance!
First, don't release the image if you use it later. Second, your output argument is pointing to non-initialized address. This is how to use initialized memory for the output address:
uint8_t* output;
datasize = WebPEncodeRGB((uint8_t*)data, width, height, step, qualityFactor, &output);
You release the image with cvReleaseImage before you try to use the pointer to the image data for the encoding. Probably that release function frees the image buffer and your data pointer now doesn't point to valid memory anymore.
This might be the reason for your segfault.
so it looks like the problem was here:
// load an image
img=cvLoadImage(argv[1]);
The function cvLoadImage takes an extra parameter
cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)
and when I changed to
img=cvLoadImage(argv[1],1);
the segfault went away.