简体   繁体   中英

Applying SVD to YCbCr image in OpenCV

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM