极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10258|回复: 0

OpenCV学习笔记之——图像变换

[复制链接]
发表于 2011-10-3 21:16:45 | 显示全部楼层 |阅读模式
本帖最后由 三水 于 2011-10-3 23:08 编辑

#include "stdafx.h"
#include "cv.h"
#include <cxcore.h>
#include <highgui.h>



void example2_4( IplImage* image )

{
    // Create some windows to show the inpu
    // and output images in.
    //
    cvNamedWindow( "Example2_4-in", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "Example2_4-out", CV_WINDOW_AUTOSIZE );   
    // Create a window to show our input image
    //
    cvShowImage( "Example2_4-in", image );   
    // Create an image to hold the smoothed output
    IplImage* out = cvCreateImage(
       cvGetSize(image),
        IPL_DEPTH_8U,
        3
    );
    // Do the smoothing
    //
    cvSmooth( image, out, CV_GAUSSIAN, 5,5 );
    cvSmooth( out, out, CV_GAUSSIAN, 5, 5);  
    // Show the smoothed image in the output window
    //
    cvShowImage( "Example2_4-out", out );
    // Be tidy
    //
    cvReleaseImage( &out );
    // Wait for the user to hit a key, then clean up the windows
    //
    cvWaitKey( 0 );
    cvDestroyWindow("Example2_4-in" );
    cvDestroyWindow("Example2_4-out" );
}



int main( int argc, char** argv )
{
  argv[1]="E:\lena.jpg";
  IplImage* img = cvLoadImage( argv[1] );
  cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example1", img );
  example2_4( img );
  cvWaitKey(0);
  cvReleaseImage( &img );
  cvDestroyWindow("Example1");
}

平滑

平滑

cvShowImage()和学习笔记之初试中无异,cvCreateImage()来为新的帧分配空间且只分配一帧图像的空间,再次调用时覆盖前一次的数据(这样每次调用返回的指针是一样的)。
cvGetsize(image)获得CvSize结构,第一个参数说明里当前图像的结构大学结构,第二个参数告诉了我们各通道每个像素点的数据类型,最后一个参数说明了通道的总数。程序中图像通道是3个(每个通道为8位)。图像大小同image。
该例程为平滑处理函数,通过使用每个像素周围3*3区域进行高斯平滑处理。

#include "stdafx.h"
#include "cv.h"
#include <cxcore.h>
#include <highgui.h>


IplImage* doPyrDown(
  IplImage* in,
  int       filter = IPL_GAUSSIAN_5x5)

{
   // Best to make sure input image is divisible by two.
    assert( in->width%2 == 0 && in->height%2 == 0 );
   IplImage* out = cvCreateImage(
        cvSize( in->width/2, in->height/2 ),
        in->depth,
        in->nChannels
    );
    cvPyrDown( in, out );
    return( out );
};

int main( int argc, char** argv )
{
  argv[1]="E:\lena.jpg";
  IplImage* img = cvLoadImage( argv[1] );
  IplImage* img2 = cvCreateImage(
cvSize( img->width/2,img->height/2 ),
img->depth, img->nChannels);
  cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example1", img );
  img2 = doPyrDown( img );
  cvShowImage("Example2", img2 );
  cvWaitKey(0);
  cvReleaseImage( &img );
  cvReleaseImage( &img2 );
  cvDestroyWindow("Example1");
  cvDestroyWindow("Example2");
}

缩放

缩放


#include "stdafx.h"
#include "cv.h"
#include <cxcore.h>
#include <highgui.h>



IplImage* doCanny(
    IplImage* in,
    double    lowThresh,
    double    highThresh,
    double    aperture)
{
    if (in->nChannels != 1)
        return(0);
// Canny only handles gray scale images
    IplImage* out = cvCreateImage(
        cvGetSize( in ),
        in->depth,
//        IPL_DEPTH_8U,   
        1);
    cvCanny( in, out, lowThresh, highThresh, aperture );
        return( out );
};

int main( int argc, char** argv )
{
  argv[1]="E:\lena.jpg";
  IplImage* img_rgb = cvLoadImage( argv[1] );
  IplImage* img_gry = cvCreateImage(
cvSize( img_rgb->width,img_rgb->height ),
img_rgb->depth,
1);
  cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
  cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example Gray", img_gry );
  IplImage* img_cny = doCanny( img_gry, 10, 100, 3 );
  cvShowImage("Example Canny", img_cny );
  cvWaitKey(0);
  cvReleaseImage( &img_rgb);
  cvReleaseImage( &img_gry);
  cvReleaseImage( &img_cny);
  cvDestroyWindow("Example Gray");
  cvDestroyWindow("Example Canny");
}

边沿及灰度

边沿及灰度


#include "stdafx.h"
#include "cv.h"
#include <cxcore.h>
#include <highgui.h>


IplImage* doCanny(
    IplImage* in,
    double    lowThresh,
    double    highThresh,
    double    aperture)
{
    IplImage* out = cvCreateImage(
        cvGetSize( in ),
        in->depth,
        //IPL_DEPTH_8U,  
        1);
    cvCanny( in, out, lowThresh, highThresh, aperture );
    return( out );
};

IplImage* doPyrDown(
  IplImage* in,
  int       filter = IPL_GAUSSIAN_5x5)
{

    // Best to make sure input image is divisible by two.
    //  assert( in->width%2 == 0 && in->height%2 == 0 );
    IplImage* out = cvCreateImage(
        cvSize( in->width/2, in->height/2 ),
        in->depth,
        in->nChannels
    );
    cvPyrDown( in, out );
    return( out );
};


int main( int argc, char** argv )
{
  IplImage* img_rgb  = cvLoadImage("E:\\lena.jpg");
  IplImage* img_gry  = cvCreateImage( cvSize( img_rgb->width,img_rgb->height ), img_rgb->depth, 1);
  cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
  IplImage* img_pyr  = doPyrDown( img_gry, IPL_GAUSSIAN_5x5 );
  IplImage* img_pyr2 = doPyrDown( img_pyr, IPL_GAUSSIAN_5x5 );
  IplImage* img_cny  = doCanny( img_pyr2, 10, 100, 3 );
  cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Pyr", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example Gray", img_gry );
  cvShowImage("Example Pyr", img_pyr2 );
  cvShowImage("Example Canny", img_cny );
  cvWaitKey(0);
  cvReleaseImage( &img_rgb);
  cvReleaseImage( &img_gry);
  cvReleaseImage( &img_pyr);
  cvReleaseImage( &img_pyr2);
  cvReleaseImage( &img_cny);
  cvDestroyWindow("Example Gray");
  cvDestroyWindow("Example Pyr");
  cvDestroyWindow("Example Canny");
}

缩放及边沿

缩放及边沿

评分

参与人数 1 +3 收起 理由
弘毅 + 3 赞一个!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2020-7-9 19:21 , Processed in 0.049426 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表