一、Opencv支持的图片格式
OpenCV目前支持的图像格式包括:
Windows位图文件 - BMP, DIB;
JPEG文件 - JPEG, JPG, JPE;
便携式网络图片 - PNG;
便携式图像格式 - PBM,PGM,PPM;
Sun rasters - SR,RAS;
TIFF文件 - TIFF,TIF;
OpenEXR HDR 图片 - EXR;
JPEG 2000 图片- jp2。
可以用cvSaveImage函数或者imwrite()函数来实现不同图像格式之间的转换,保存时的后缀名就是对应的图片格式。
二、Opencv图像格式转换
opencv 的图片导入(cvLoadImage)和保存图片(cvSaveImage)分别支持很多格式的图片,但是他们在内存在却是统一的结构( IplImage),所以我们可以很轻松的实现图片格式的转换,代码很简单
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<stdlib.h>
using namespace std;
using namespace cv;
int main(int argc, char * argv[])
{
IplImage* img = 0;
img = cvLoadImage(argv[1], CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
cvNamedWindow("region of image", CV_WINDOW_AUTOSIZE);
cvShowImage("region of image", img);
int num = strlen(argv[1]);
char* name = new char[num + 1];
strcpy(name, argv[1]);
*(name + num - 1) = 'M';
*(name + num - 2) = 'G';
*(name + num - 3) = 'P';
*(name + num) = '\0';
imwrite(name, img);
cvWaitKey(0);
return 0;
}
}
三、Opencv黑白棋盘生成
#include<iostream>
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace std;
const int perBoardPixel = 100;
const cv::Size boardSize(8, 8);
const cv::Size resolution(800, 800);
int main()
{
int basisHeight = (resolution.height - perBoardPixel*boardSize.height) / 2;
int basisWidth = (resolution.width - perBoardPixel*boardSize.width) / 2;
if (basisHeight < 0 || basisWidth < 0)
{
cout << "Resolution doesn't match!" << endl;
}
cv::Mat image(resolution, CV_16UC1, cv::Scalar::all(1023));//white
int flag = 0;
for (int j = 0; j < boardSize.height; j++)
{
for (int i = 0; i < boardSize.width; i++)
{
flag = (i + j) % 2;
if (flag == 0)
{
for (int n = j * perBoardPixel; n < (j + 1) * perBoardPixel; n++)
for (int m = i * perBoardPixel; m < (i + 1) * perBoardPixel; m++)
//image.at<uchar>(n + basisHeight, m + basisWidth) = 0;
image.at<ushort>(n + basisHeight, m + basisWidth) = 0;
}
}
}
cv::imwrite("chessBoard1.pgm", image);
//cv::waitKey(0);
return 0;
}