gpt4 book ai didi

c++ - 使用 openCV 锐化视频图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:56:32 26 4
gpt4 key购买 nike

我想使用 OpenCV 锐化我的图像,我在网上查看了一个对灰度图像执行锐化的示例,我尝试了一下,效果很好。然而,我现在正尝试做同样的事情,但使用 RGB 颜色,所以我在三个 channel 上分别执行相同的功能,但它没有给我任何结果图像与原始图像完全一样。

#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);
}

最佳答案

I am now trying to do the same but with RGB colors so I am performing the same functionality

检查你的假设!我不认为你在做同样的事情。我不知道openCV很好,但是你没有对你的临时图像做任何事情,所以没有理由改变 channel 图像!您应该在原始图像中设置临时图像的结果,也许是这样的:

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 包含的内容,并查看在您的应用中重用它时遗漏了什么。

关于c++ - 使用 openCV 锐化视频图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8396140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com