cvDecodeImage fail in a cgi program - opencv

In a single application, the following code is ok
CvMat src_image_mat;
cvInitMatHeader(&src_image_mat,1,src_image_data.size(), \
CV_8U,(void *)src_image_data.c_str());
m_pSrcImage = cvDecodeImage(&src_image_mat, 0);
where src_image_data contains all the bytes in a given jpeg file,
after calling ,m_pSrcImage is not NULL.
but when this code is run in a cgi program, the value cvDecodeImage returns is NULL, and the src_image_data is the same as in the single application.
btw: when in cgi context, the picture is uploaded by some users.

assure file is already read into memory to pData with size nSize.
CvMat cm = cvMat(1, nSize, CV_8UC1, pData);
pImage = cvDecodeImage(&cm);

Related

Bazel build NodeJS C++ Addon

I've created a sample library with Bazel: https://github.com/rynz/test-app
How do I go about building this library in a NodeJS C++ Addon?
Can I build a NodeJS addon with Bazel? Else, what are the steps to include a Bazel library with node-gyp?
Cheers,
Ryan
Nodejs addons are encapsulated in their own side, you have to do the connections.To work with an addon you need to convert all js arguments to c/c++, so you can work with them.
napi string example:
napi_value argv[1];
size_t argc = 1;
char* str[LENGTH];
napi_get_value_string_utf8(
env //environment
, argv[0] //napi_value represents a js string
, (char*) &str //(c/c++)buffer to store the utf8 string
, sizeof(str) //dest buffer size
, &argc // src buffer size
);
//now you can work with str in c/c++
//Do your stuff here
After the addon work you need to convert the c/c++ return values to js values.
Returning the string:
napi_value result;
napi_create_string_utf8(
env
, (char*) &str //(c/c++)buffer to convert to napi
, NAPI_AUTO_LENGTH //buffer size AUTO_LENGTH if its null-terminated
, &result //the resulting napi string
);
//now you can return result as napi_value
Check the complete docs here https://nodejs.org/api/n-api.html

loading image in opencv2.4.6

In opencv 2.4.6. I am trying to load a mat image file with a simple code given below. But the image is not loaded as I print the image size, it is showing '0'. Can anybody please tell me , what is going wrong?
int main(int argc, char argv[])
{
Mat a=imread("C:/image3.jpg");
cv::Size frame11_size = a.size();
printf("%d",frame11_size.height);
return 0;
}
Update: I solved the problem. The problem was, I was only including all the library,include and additional dependencies in 'debug mode' only. I did not change anything in 'release mode'. When I change the properties in 'release mode' as-well, it worked. thanks all for your kind responses, I am giving '+1' for your answers.
I think there should be single slash on your image path, and always check whether image is successfully loaded.
Mat a=imread("C:/image3.jpg");
if(! a.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
OpenCV can't open jpg files by itself. It depends on third parties to do so. Maybe you are missing certain dlls, or maybe your OpenCV installation don't have the right path to them. To test this assumption store your image in other formats. For example pgm or ppm. Those formats does not perform any encoding and just store image buffer in file as is. As a result OpenCV will not need any external libraries to open image in ppm format.

Opencv - create png image

As part of my project I wanted to send stream of images using websockets from embedded machine to client application and display them in img tag to achieve streaming.
Firstly I tried to send raw RGB data (752*480*3 - something about 1MB) but in the end I got some problems with encoding image to png in javascript based on my RGB image so I wanted to try to encode my data to PNG firstly and then sent it using websockets.
The thing is, I am having some problems with encoding my data to PNG using OpenCV library that is already used in the project.
Firstly, some code:
websocketBrokerStructure.matrix = cvEncodeImage(0, websocketBrokerStructure.bgrImageToSend, 0);
websocketBrokerStructure.imageDataLeft = websocketBrokerStructure.matrix->rows * websocketBrokerStructure.matrix->cols * websocketBrokerStructure.matrix->step;
websocketBrokerStructure.imageDataSent = 0;
but I am getting strange error during execution of the second line:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
and I am a bit confused why I am getting this error from my code.
Also I am wondering if I understand it right: after invoking cvEncodeImage (where bgrImage is IplImage* with 3 channels - BGR) I just need to iterate through data member of my CvMatto get all of the png encoded data?
The cvEncodeImage function takes as its first parameter the extension of the image you want to encode. You are passing 0, which is the same thing as NULL. That's why you are getting the message NULL not valid.
You should probably use this:
websocketBrokerStructure.matrix = cvEncodeImage(".png", websocketBrokerStructure.bgrImageToSend, 0);
You can check out the documentation of cvEncodeImage here.
You can check out some examples of cvEncodeImage, or its C++ brother imencode here: encode_decode_test.cpp. They also show some parameters you can pass to cvEncodeImage in case you want to adjust them.

TBitmap->LoadFromStream failed with Win XP

I'm using C++ Builder XE3 to develop a graph editor. All of the editing and drawing capabilities are made in a DLL that is loaded by the end user applications.
To store information about the available graph objects I use a SQLite database. That database contains BMP icons that are loaded into a TImageList at run-time.
Everything works fine with Win-7, Win-8 and Win-vista but with Win-XP a "Floating point division by 0" occurs when loading the bitmap. I use a temporary memory stream to load the blob from the database and then load it into a temporary TBitmap which is used to add the new icon into the final TImageList.
Here is the function used to do so...
void TIcons::AddMaskedBitmap( TImageList *ptImgList, unsigned char *pucIcon, unsigned int uiSize )
{
TMemoryStream *ptMemStream;
// Use a memory stream
ptMemStream = new TMemoryStream();
ptMemStream->Write( pucIcon, uiSize );
ptMemStream->Position = 0;//Seek( ( int )0, ( unsigned short )soBeginning );
// Load using the cached bmp object
m_ptBmp->Transparent = true;
#warning "floatting point division by 0 error with WinXP"
m_ptBmp->LoadFromStream( ptMemStream ); // floatting point division by 0 error with WinXP
// m_ptBmp->LoadFromFile( ".\\d.bmp" ); // works
// Create a mask
m_ptBmpMask->Assign( m_ptBmp );
m_ptBmpMask->Canvas->Brush->Color = m_ptBmp->TransparentColor;
m_ptBmpMask->Monochrome = true;
// Add it to the list
ptImgList->Add( m_ptBmp, m_ptBmpMask );
// Free mem
m_ptBmp->FreeImage();
m_ptBmpMask->FreeImage();
delete ptMemStream;
}
I've traced the TBitmap::LoadFromStream function and the exception occurs in the CreateDIBSection function.
To make sure the loaded bitmap files are saved using the right encoding I've tried to load them using the TBitmap::LoadFromFile function and it works fine, so I think there's something wrong with the TBitmap::LoadFromStream function but I can't figure out what !
If anyone has an idea...
Thanks.
LoadFromFile is implemented by creating a file stream, and passing that to LoadFromStream. This, if the contents of your memory stream are the same as the contents of the file, then the call to LoadFromStream will succeed.
Thus the only sane conclusion is that the contents of the memory stream are invalid in some way.
The bitmap stored into the database is encoded using the BITMAPV4HEADER structure which is supposed to be supported since Win95/NT4 but there's something wrong.
It works fine if I encode the bitmap using the BITMAPINFOHEADER structure which is an older version of bitmap encoding which does not contain color space information.
Just found out a solution that works for me.
My problem was that software that was developed on Win7, when running on XP was throwing the division by 0 error when loading one of my BMPs.
It turns out that the problematic BMP was saved using Win7 Paint (other BMPs that were ok were saved from Gimp).
All I needed to do to fix it was to open this BMP on XP Paint and save it from there.

cvLoadImage works with string constant but not c_str()

I am using cvloadimage to open an image in my program. The images are in a external directory, generated using the current time in the filename.
When I try to load images in this way:
IplImage *image = cvLoadImage(path.c_str,CV_LOAD_IMAGE_COLOR);
the image structure will be NULL and the application will stop with q segmentation fault.
When I try to load an image this way:
IplImage *image = cvLoadImage("path/images/image_2012_11_25.jpg",CV_LOAD_IMAGE_COLOR);
then it works great.
Is the problem that cvLoadImage can't accept any type of text, only const char*?
But c_str() converts string to char*, right?
How can I solve this problem?
I had the same problem with writing into a file so I used the code below:
const char *M=s.c_str();
file.write(M,s.size());
hope it works...

Resources