2011-12-06 184 views
4

我想使用OpenCV来锐化我的图像,并且我已经在线查看了一个在灰度图像上执行锐化的示例,并且我尝试了它,并且它工作得很完美。然而,我现在正在尝试做同样的事情,但使用RGB颜色,因此我在三个通道上分别执行相同的功能,但没有给出任何结果,图像与原始图像完全相同。使用openCV来锐化视频图像

#include "Sharpening.h" 
using namespace std; 

Sharpening::Sharpening() { 
} 

Sharpening::~Sharpening() { 
} 

IplImage* Sharpening::laplace(IplImage* channel) { 

    CvSize size = cvSize(channel->width, channel->height); 

    IplImage* temp = cvCreateImage(size, IPL_DEPTH_8U, 1); 

    IplImage* lapl = cvCreateImage(size, IPL_DEPTH_8U, 1); 

    int width = size.width; 

    int height = size.height; 

    cvConvertScale(channel, temp, 1.0); 

    CvMat* ker = cvCreateMat(3, 3, CV_32FC1); 

    cvSet(ker, cvScalarAll(-1.0)); 

    cvSet2D(ker, 1, 1, cvScalarAll(15.0)); 

    cout << "this is been executed"; 

    cvFilter2D(temp, lapl, ker); 

    cvReleaseMat(&ker); 

    double maxv = 0.0; 

    float maxFloat = 1.79769e+308; 

    double minv = maxFloat; 

    cvMinMaxLoc(lapl, &minv, &maxv); 


    for (int i = 0; i < width * height; i++) { 

     double lap_val = cvGet1D(lapl, i).val[0]; 

     int v = (int) ((255.0 * lap_val/maxv) + 0.5); // this calculation does nothing particularly 

     cvSet1D(temp, i, cvScalarAll(v)); 
    } 

    maxv = 0.0; 

    cvMinMaxLoc(channel, &minv, &maxv); 

    for (int i = 0; i < width * height; i++) { 

     double val = cvGet1D(channel, i).val[0]; 
     int v = (int) ((255.0 * val/maxv) + 0.5); 

     cvSet1D(channel, i, cvScalarAll(v)); 
    } 

    cvReleaseImage(&temp); 
    cvReleaseImage(&lapl); 
    cvReleaseMat(&ker); 

    return channel; 
} // end of function 

int Sharpening::calculateLoop(int number) { 
    int value = 2; 
    for (int i = 0; i < 10; i++) { 
     number = number * value; 
     cout << number << endl; 
    } 

    return number; 
} 

//====================================================================================== 

int Sharpening::SharpenColored(Sharpening sharp) { 

    int key = 0; 

    CvCapture *capture = 0; 

    IplImage* frame = 0; 

    cvNamedWindow("deblur", CV_WINDOW_AUTOSIZE); 

    cvNamedWindow("deblur2", CV_WINDOW_AUTOSIZE); 

    cvNamedWindow("origional", CV_WINDOW_AUTOSIZE); 

    // initialize camera 
    capture = cvCaptureFromCAM(0); //capture from a camera 

    //capture = cvCaptureFromAVI("jabelH2.avi"); 

    //frame = cvQueryFrame(capture); 

    if (!cvGrabFrame(capture)) { // capture a frame 
     printf("Could not grab a frame\n\7"); 
     exit(0); 
    } 

    frame = cvQueryFrame(capture); 

    CvSize imageSize1 = cvSize(frame->width, frame->height); 

    IplImage* R = cvCreateImage(imageSize1, IPL_DEPTH_8U, 1); 

    IplImage* G = cvCreateImage(imageSize1, IPL_DEPTH_8U, 1); 

    IplImage* B = cvCreateImage(imageSize1, IPL_DEPTH_8U, 1); 

    IplImage* R2 = cvCreateImage(imageSize1, IPL_DEPTH_8U, 1); 

    IplImage* G2 = cvCreateImage(imageSize1, IPL_DEPTH_8U, 1); 

    IplImage* B2 = cvCreateImage(imageSize1, IPL_DEPTH_8U, 1); 

    IplImage* source = cvCreateImage(imageSize1, IPL_DEPTH_8U, 3); 

    IplImage* result = cvCreateImage(imageSize1, IPL_DEPTH_8U, 3); 

    IplImage* result2 = cvCreateImage(imageSize1, IPL_DEPTH_8U, 3); 

    QFuture<IplImage*> future1; 

    QFuture<IplImage*> future2; 

    QFuture<IplImage*> future3; 

    while (key != 'q') { 
     // get a frame 

     frame = cvQueryFrame(capture); 

     // always check 
     if (!frame) 
      break; 

     source = frame; 

     cvSplit(frame, B, G, R, NULL); 

     future1 = QtConcurrent::run(sharp, &Sharpening::laplace, R); 

     future2 = QtConcurrent::run(sharp, &Sharpening::laplace, G); 

     future3 = QtConcurrent::run(sharp, &Sharpening::laplace, B); 

     R2 = future1.result(); 

     G2 = future2.result(); 

     B2 = future3.result(); 

     cvMerge(B2, G2, R2, NULL, result); 

     cvAdd(source, result, result2, NULL); 
     cvShowImage("origional", source); 
     cvShowImage("deblur", R2); 
     cvShowImage("deblur2", G2); 

     key = cvWaitKey(1); 
    } //end of while 

    cvDestroyWindow("deblur"); 
    cvDestroyWindow("deblur2"); 
    cvDestroyWindow("origional"); 
    cvReleaseImage(&R); 
    cvReleaseImage(&source); 
    cvReleaseImage(&R2); 
    cvReleaseImage(&G); 
    cvReleaseImage(&G2); 
    cvReleaseImage(&B); 
    cvReleaseImage(&B2); 
    cvReleaseImage(&result); 
    cvReleaseImage(&result2); 
    cvReleaseCapture(&capture); 
    delete future1; 
    delete future2; 
    delete future3; 

    return 0; 
} //end of function 

//====================================================================================== 

int main(int argc, char *argv[]) { 
    Sharpening sh; 
    sh.SharpenColored(sh); 
} 

回答

4

我现在试图做相同,但与RGB颜色,所以我进行 相同的功能

检查你的假设!我不认为你也这样做。我不太了解openCv ,但是您的临时图像没有做任何处理,所以没有理由改变通道图像!您应设置临时图像的结果,在原始图像中,这样的事情或许是:

for (int i = 0; i < width * height; i++) { 

    double lap_val = cvGet1D(lapl, i).val[0]; // get modified image data 

    int v = (int) ((255.0 * lap_val/maxv) + 0.5); // scale to 0 255 
    cvSet1D(channel, i, cvScalarAll(v)); // store in original image 
} 

或者你可以把原来的代码和注释它来解释每个cvImage包含,看看你错过了什么当你的应用程序重用它。

+0

oppss,你其实是正确的,我应该把频道,而不是温度这里..我坏:| – user573014

+1

@ user573014选择此答案作为答案的正式答案,方法是单击答案旁边的复选框。 – karlphillip

+0

好的我做到了,谢谢 – user573014