顯示具有 opencv 標籤的文章。 顯示所有文章
顯示具有 opencv 標籤的文章。 顯示所有文章

2009年5月18日 星期一

OpenCV - 縮小圖像

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main()
{
char FileName[10]="rain.jpg";

// 來源指標
IplImage *src = 0;

// 目的指標
IplImage *dst = 0;

// 縮小倍數
float scale = 0.618;

// 目標圖像的大小
CvSize dst_cvsize;

// 載入圖檔
src = cvLoadImage("rain.jpg",1);

// 設定目標寬為100 pixel
dst_cvsize.width = 100;
// 同比例縮小
dst_cvsize.height = src->height * ((float) dst_cvsize.width/src->width);

// 建構目標圖像
dst = cvCreateImage(dst_cvsize,src->depth,src->nChannels);

// 縮小來源圖到目標圖像
cvResize(src,dst,CV_INTER_LINEAR);

// 列印出原圖與縮小後圖的大小
printf("the original size is %i\n",src->imageSize);
printf("the scaled size is %i\n",dst->imageSize);

cvNamedWindow("src",CV_WINDOW_AUTOSIZE);

cvNamedWindow("dst",CV_WINDOW_AUTOSIZE);

// 顯示來源圖像
cvShowImage("src",src);

// 顯示目標圖像
cvShowImage("dst",dst);

// 等待用戶反應
cvWaitKey(-1);

// 釋放來源圖占用的記憶體
cvReleaseImage(&src);

// 釋放目標圖占用的記憶體
cvReleaseImage(&dst);

return 0;
}


編譯方式:
$ gcc `pkg-config opencv --libs --cflags` main.c -o main

參考資料:
縮放圖片

OpenCV - 如何將IplImage型態的image轉換為unsigned char的形式供後續處理?

// start capturing frames from camera
// 0:autodetect

CvCapture *camera = cvCreateCameraCapture(0);
// 確認camera不為NULL
assert(camera);
IplImage *image = cvQueryFrame(camera);
// 確認camera不會NULL
assert(image);
// 求得image的影像大小資訊
CvSize imgSize;
imgSize = cvGetSize(image);
// 將IplImage型態的 image 轉換為 BYTE的image_rawdata
void *image_rawdata;
cvGetImageRawData( image, ( uchar** )&image_rawdata, 0, &imgSize );
// image 是3個channel (RGB) image_rawdata 的大小為 Height * Width * 3
for(j = 0; j < imgSize.height ; j ++) for(i = 0; i < imgSize.width ; i ++)
{
// 取得第(i,j) pixel 的 R、G、B值
B = image_rawdata[(j * imgSize.width + i) * 3 + 2];
G = image_rawdata[(j * imgSize.width + i) * 3 + 1];
R = image_rawdata[(j * imgSize.width + i) * 3 + 0];
}


參考資料:
OpenCV DIY手冊

OpenCV相關函式與資料結構

這一個是在opencv上很常用的資料結構

資料結構都放在/usr/include/opencv/cxtypes.h

/****************************************************************************************\
* Image type (IplImage) *
\****************************************************************************************/


#ifndef HAVE_IPL

/*
* The following definitions (until #endif)
* is an extract from IPL headers.
* Copyright (c) 1995 Intel Corporation.
*/

#define IPL_DEPTH_SIGN 0x80000000

#define IPL_DEPTH_1U 1
#define IPL_DEPTH_8U 8
#define IPL_DEPTH_16U 16
#define IPL_DEPTH_32F 32

#define IPL_DEPTH_8S (IPL_DEPTH_SIGN| 8)
#define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16)
#define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32)

#define IPL_DATA_ORDER_PIXEL 0
#define IPL_DATA_ORDER_PLANE 1

#define IPL_ORIGIN_TL 0
#define IPL_ORIGIN_BL 1

#define IPL_ALIGN_4BYTES 4
#define IPL_ALIGN_8BYTES 8
#define IPL_ALIGN_16BYTES 16
#define IPL_ALIGN_32BYTES 32

#define IPL_ALIGN_DWORD IPL_ALIGN_4BYTES
#define IPL_ALIGN_QWORD IPL_ALIGN_8BYTES

#define IPL_BORDER_CONSTANT 0
#define IPL_BORDER_REPLICATE 1
#define IPL_BORDER_REFLECT 2
#define IPL_BORDER_WRAP 3

typedef struct _IplImage
{
int nSize; /* sizeof(IplImage) */
int ID; /* version (=0)*/
int nChannels; /* Most of OpenCV functions support 1,2,3 or 4 channels */
int alphaChannel; /* ignored by OpenCV */
int depth; /* pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported */

char colorModel[4]; /* ignored by OpenCV */
char channelSeq[4]; /* ditto */
int dataOrder; /* 0 - interleaved color channels, 1 - separate color channels.
cvCreateImage can only create interleaved images */

int origin; /* 0 - top-left origin,
1 - bottom-left origin (Windows bitmaps style) */

int align; /* Alignment of image rows (4 or 8).
OpenCV ignores it and uses widthStep instead */

int width; /* image width in pixels */
int height; /* image height in pixels */
struct _IplROI *roi;/* image ROI. if NULL, the whole image is selected */
struct _IplImage *maskROI; /* must be NULL */
void *imageId; /* ditto */
struct _IplTileInfo *tileInfo; /* ditto */
int imageSize; /* image data size in bytes
(==image->height*image->widthStep
in case of interleaved data)*/

char *imageData; /* pointer to aligned image data */
int widthStep; /* size of aligned image row in bytes */
int BorderMode[4]; /* ignored by OpenCV */
int BorderConst[4]; /* ditto */
char *imageDataOrigin; /* pointer to very origin of image data
(not necessarily aligned) -
needed for correct deallocation */

}
IplImage;


函式都放在/usr/include/opencv/cxcore.h中

/* Returns width and height of array in elements */
(CvSize) cvGetSize( const CvArr* arr );

/usr/include/opencv/highgui.h

/* start capturing frames from camera: index = camera_index + domain_offset (CV_CAP_*) */
(CvCapture*) cvCreateCameraCapture( int index );

/* Returns width and height of array in elements */
CVAPI(CvSize) cvGetSize( const CvArr* arr );

/usr/include/opencv/cxtypes.h
// CvArr就是void
typedef void CvArr;

/* Retrieves raw data of CvMat, IplImage or CvMatND. In the latter case the function raises an error if the array can not be represented as a matrix */
把IplImage轉換成uchar資料陣列
(void) cvGetRawData( const CvArr* arr, uchar** data, int* step CV_DEFAULT(NULL), CvSize* roi_size CV_DEFAULT(NULL));
(void) cvGetImageRawData( const CvArr* arr, uchar** data, int* step CV_DEFAULT(NULL), CvSize* roi_size CV_DEFAULT(NULL));

2009年2月18日 星期三

OpenCV

example01
讀取圖片
main.c

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main()
{
char FileName[10]="rain.jpg";
IplImage *Image1 = cvLoadImage(FileName,1);
cvNamedWindow("Show Image",0);
cvResizeWindow("Show Image",300,400);
cvShowImage("Show Image",Image1);
cvWaitKey(0);
cvDestroyWindow("Show Image");
cvReleaseImage(&Image1);
}

編譯方式
$ gcc `pkg-config opencv --libs --cflags` main.c -o main

example02
讀取webcam
main.c
#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main()
{
CvCapture *capture;
IplImage *frame;
capture =cvCaptureFromCAM(0) ;
cvNamedWindow("Webcam",0);
while(1)
{
frame = cvQueryFrame(capture);
cvShowImage("Webcam",frame);
if(cvWaitKey(10)>=0)
{
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("Webcam");
}

編譯方式
$ gcc `pkg-config opencv --libs --cflags` main.c -o main

參考資料:
OpenCV中文網站
HighGUI Reference Manual
opencv教學網頁
HighGUI參考手冊
Introduction to programming with OpenCV

其它資料:
cvGetRawData()的奇怪問題
ArtificialWistom: OpenCV
opencv編程入門4