OpenCV学习06

来源:互联网 发布:富云软件科技有限公司 编辑:程序博客网 时间:2024/05/29 18:01

导出边缘检测后的图像

////  main.cpp//  Study06////  Created by Sean on 16/2/21.//  Copyright © 2016年 Sean. All rights reserved.//#include <iostream>#include "highgui.h"#include "cv.h"using namespace std;IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture){    if(in->nChannels!=1)        return(0);    IplImage* out = cvCreateImage(cvSize(in->width,in->height), IPL_DEPTH_8U, 1);    //IplImage* out = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);    cvCanny(in, out, lowThresh, highThresh, aperture);    return out;}int main(int argc, const char * argv[]) {    // insert code here...    cout << "Hello, World!\n";    IplImage* in = cvLoadImage("/Users/sean/Pictures/11.png");    IplImage* in2 = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);    cvCvtColor(in,in2,CV_BGR2GRAY);    char A[]="in_re",B[]="in_af",C[]="out";    cvNamedWindow(A);    cvNamedWindow(B);    cvNamedWindow(C);    cvShowImage(A, in);    cvShowImage(B, in2);    cvShowImage(C, doCanny(in2, 50, 200, 3));    while(cvWaitKey(0)!=27);    cvReleaseImage(&in);    cvDestroyAllWindows();    return 0;}

这里发现个问题,就是cvCanny的作用对象必须是单通道的图像结构,(普及知识:OpenCV中所有的数据结构都是以结构体的形式实现,并以结构体指针的形式传递。这就能解释为什么每次申请图像空间的时候是“IplImage* img;”了),也就是说,图像结构体中的nChannels必须为1才能准确的实行阈值边缘检测,虽然在nChannels为3的时候(色彩的三个通道)也能用cvCanny,但所得的两个结果有所不同。下面给出nChannels为3的时候的代码:


////  main.cpp//  Study06////  Created by Sean on 16/2/21.//  Copyright © 2016年 Sean. All rights reserved.//#include <iostream>#include "highgui.h"#include "cv.h"using namespace std;IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture){//    if(in->nChannels!=1)//        return(0);    IplImage* out = cvCreateImage(cvSize(in->width,in->height), IPL_DEPTH_8U, 1);    //IplImage* out = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);    cvCanny(in, out, lowThresh, highThresh, aperture);    return out;}int main(int argc, const char * argv[]) {    // insert code here...    cout << "Hello, World!\n";    IplImage* in = cvLoadImage("/Users/sean/Pictures/11.png");    IplImage* in2 = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);    cvCvtColor(in,in2,CV_BGR2GRAY);    char A[]="in_re",B[]="in_af",C[]="out_1",D[]="out_3";    cvNamedWindow(A);    cvNamedWindow(B);    cvNamedWindow(C);    cvNamedWindow(D);    cvShowImage(A, in);    cvShowImage(B, in2);    cvShowImage(C, doCanny(in2, 50, 200, 3));    cvShowImage(D, doCanny(in, 50, 200, 3));    while(cvWaitKey(0)!=27);    cvReleaseImage(&in);    cvDestroyAllWindows();    return 0;}



0 0