
之前已经使用过图像载入了,这里再讲述一下其他的一些tip。
先来一次普通的载入:
#include <cv.h>
#include <highgui.h> int main(int argc, char **argv)
{
IplImage *image = cvLoadImage(argv[1]);
//CV_WINDOW_AUTOSIZE == 1
cvNamedWindow(argv[1], CV_WINDOW_AUTOSIZE); cvShowImage(argv[1], image);
cvWaitKey(0);
cvDestroyWindow(argv[1]);
cvReleaseImage(&image); return 0;
}
这里要注意,这个窗口大小是不可以改变的,下面再修改代码:
gedit ./modules/highgui/include/opencv2/highgui/highgui_c.h
发现:
//These 2 flags are used by cvNamedWindow and cvSet/GetWindowProperty
CV_WINDOW_NORMAL = 0x00000000, //the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size
CV_WINDOW_AUTOSIZE = 0x00000001, //the user cannot resize the window, the size is constrainted by the image displayed
CV_WINDOW_OPENGL = 0x00001000, //window with opengl support
#include <cv.h>
#include <highgui.h> int main(int argc, char **argv)
{
IplImage *image = cvLoadImage(argv[1]);
//CV_WINDOW_AUTOSIZE == 1
//CV_WINDOW_NORMAL == 0
cvNamedWindow(argv[1], <strong>CV_WINDOW_NORMAL</strong>); cvShowImage(argv[1], image);
cvWaitKey(0);
cvDestroyWindow(argv[1]);
cvReleaseImage(&image); return 0;
}
这样就可以随便拖放大小了(右下角的地方拖拽)
然后再加料:
#include <cv.h>
#include <highgui.h> int main(int argc, char **argv)
{
IplImage *image = cvLoadImage(argv[1]);
//CV_WINDOW_AUTOSIZE == 1
//CV_WINDOW_NORMAL == 0
cvNamedWindow(argv[1], CV_WINDOW_NORMAL); cvShowImage(argv[1], image); <strong> cvResizeWindow(argv[1], 200, 100);</strong> cvWaitKey(0);
cvDestroyWindow(argv[1]);
cvReleaseImage(&image); return 0;
}
这里在代码里面设置了大小,不用亲手操刀了。
继续加料:
#include <cv.h>
#include <highgui.h> int main(int argc, char **argv)
{
IplImage *image = cvLoadImage(argv[1]);
//CV_WINDOW_AUTOSIZE == 1
//CV_WINDOW_NORMAL == 0
cvNamedWindow(argv[1], CV_WINDOW_NORMAL); cvShowImage(argv[1], image); cvResizeWindow(argv[1], 200, 100); <strong> cvMoveWindow(argv[1], 500 ,500);</strong> cvWaitKey(0);
<strong> cvDestroyAllWindows();</strong>
cvReleaseImage(&image); return 0;
}
这里需要自己体验一下,将图像窗口移动到屏幕某个地方,而且关闭窗口可以批量关闭。
接下来是关于图像形式的操作:
#include <cv.h>
#include <highgui.h> int main(int argc, char **argv)
{
IplImage *image = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
//CV_WINDOW_AUTOSIZE == 1
//CV_WINDOW_NORMAL == 0
cvNamedWindow(argv[1], CV_WINDOW_NORMAL); cvShowImage(argv[1], image); cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&image); return 0;
}
变黑白了,实际上是被转成了单通道,但是没有偏蓝偏绿,估计是进行了彩色到灰度的转换,详情还要看代码:
enum
{
/* 8bit, color or not */
CV_LOAD_IMAGE_UNCHANGED =-1,
/* 8bit, gray */
CV_LOAD_IMAGE_GRAYSCALE =0,
/* ?, color */
CV_LOAD_IMAGE_COLOR =1,
/* any depth, ? */
CV_LOAD_IMAGE_ANYDEPTH =2,
/* ?, any color */
CV_LOAD_IMAGE_ANYCOLOR =4
};
// CoreGraphics will take care of converting to grayscale and back as long as the
// appropriate colorspace is set
if( color == CV_LOAD_IMAGE_GRAYSCALE )
{
colorSpace = CGColorSpaceCreateDeviceGray();
bpp = 1;
alphaInfo = kCGImageAlphaNone;
}
暂时没找到此函数的内部定义。