Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/sky_person/article/details/91349146
Related header file:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs/imgcodecs_c.h>
Display a picture to load:
std::string imgpath = "D:/lenna.jpg";
IplImage* imgsrc = cvLoadImage(imgpath.c_str());
cvShowImage("ImageProc", imgsrc);
cv::waitKey(0);
cvReleaseImage(&imgsrc);
cvDestroyWindow("ImageProc");
Pictures loaded way:
cv::Mat in_image;
std::string imgpath = "D:/lenna.jpg";
in_image = cv::imread(imgpath.c_str());
if (in_image.empty()) {
std::cout << "Error! Input image cannot be read...\n";
return -1;
}
cv::imshow("ImageProc", in_image);
cv::waitKey(0);
Open the camera:
cv::VideoCapture cap(0); //打开默认的摄像头号
if (!cap.isOpened()) //检测是否打开成功
return -1;
cv::Mat frame;
cv::namedWindow("ImageProc", 1);
for (;;)
{
cap >> frame; // 从摄像头中获取新的一帧
imshow("edges", frame);
if (cv::waitKey(30) >= 0) break;
}
Simple Gaussian blur processed image example:
std::string imgpath = "D:/lenna.jpg";
IplImage* imgsrc = cvLoadImage(imgpath.c_str());
IplImage* out = cvCreateImage(cvGetSize(imgsrc), IPL_DEPTH_8U, 3);
cvSmooth(imgsrc, out, CV_GAUSSIAN, 5, 5);
cvShowImage("ImageProc", out);
cv::waitKey(0);
cvReleaseImage(&out);
cvDestroyWindow("ImageProc");
Pictures of the original image to generate a half of the image cvCreateImage, cvPyrDown
IplImage* imgsrc = cvLoadImage(imgpath.c_str());
IplImage* out = cvCreateImage(cvSize(imgsrc->width/2,imgsrc->height/2), imgsrc->depth, imgsrc->nChannels);
cvPyrDown(imgsrc, out);
cvShowImage("ImageProc", out);
cv::waitKey(0);
cvReleaseImage(&out);
cvDestroyWindow("ImageProc");
Candy edge detection example
double lowThresh = 10, highThresh = 100, aperture = 3;
IplImage* imgsrc = cvLoadImage(imgpath.c_str());
IplImage* out = cvCreateImage(cvGetSize(imgsrc), imgsrc->depth, 1);
cvCanny(imgsrc, out, lowThresh, highThresh, aperture);
cvShowImage("ImageProc", out);
cv::waitKey(0);
cvReleaseImage(&out);
cvDestroyWindow("ImageProc");
Combining operation: scaling twice, then edge detection Candy
IplImage* doPyrDown(IplImage* in) {
IplImage* out = cvCreateImage(cvSize(in->width/2,in->height/2),in->depth,in->nChannels);
cvPyrDown(in, out);
return out;
}
/* * *
* canny 处理图必须为灰度图
* * */
IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture) {
if (in->nChannels != 1)
return 0;
IplImage* out = cvCreateImage(cvGetSize(in), in->depth, 1);
cvCanny(in, out, lowThresh, highThresh, aperture);
return out;
}
int main(int argc, char** argv)
{
std::string imgpath = "D:/Engine/ImageProc/x64/Debug/lenna.jpg";
IplImage* imgsrc = cvLoadImage(imgpath.c_str());
IplImage* t1 = doPyrDown(imgsrc);
IplImage* t2 = doPyrDown(t1);
IplImage* t3 = doCanny(t2,10,100,3);
cvShowImage("ImageProc", t3);
cv::waitKey(0);
cvReleaseImage(&imgsrc);
cvReleaseImage(&t1);
cvReleaseImage(&t2);
cvReleaseImage(&t3);
cvDestroyWindow("ImageProc");
}