我想使用OpenCV锐化我的图像,我在网上查看了一个在灰度图像上执行锐化的示例,我尝试了它并且它工作得很好。但是,我现在尝试使用RGB颜色,所以我在三个通道上分别执行相同的功能,但它没有给我任何结果,图像与原始图像完全一样。
#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);
}
答案 0 :(得分: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包含的内容,并查看在应用中重复使用时遗漏的内容。