IplImage与Mat类,C语言opencv编程

下面函数是在vs2013工程下的.c文件下实现的,由于c语言与c++的差异,使得.c文件不能使用Mat类
程序可以作为纯C语言下使用opencv得实例:

#include"stdio.h"
#include <cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

//using namespace cv;
//using namespace std;
#include <cv.h>  
#include <highgui.h>  

//Mat ImgConverse(Mat &img)
//{
//	int rownum = img.rows;
//	int colnum = img.cols;
//	Mat newimg(img.rows, colnum / 2, CV_16UC1);
//
//	for (int i = 0; i < rownum; i++)  //行循环
//	{
//		uchar* data = img.ptr<uchar>(i);
//		ushort* newdata = nullptr;
//		if (i < rownum / 2)
//		{
//			newdata = newimg.ptr<ushort>(2 * i + 1);
//		}
//		else
//		{
//			newdata = newimg.ptr<ushort>(2 * (i - (rownum / 2)));
//		}
//		for (int j = 0; j < colnum; j++)
//		{
//			if (j % 2 == 1)
//			{
//				newdata[j / 2] = (data[j - 1] << 8) | data[j]; // (a<<8)|b;
//			}
//		}
//	}
//	return newimg;
//}

IplImage* ImgConverse(IplImage* imgSrc)  //函数实现将乱序的图像数据(行:相邻2字节2个像素合并为1个像素,uchar2ushort,列:前半部分基数列,后半部分为偶数列),重新组合
{
	int rownum = imgSrc->height;
	int colnum = imgSrc->width;
	int m = 0;
	IplImage *image = cvCreateImage(cvSize(colnum / 2, rownum), IPL_DEPTH_16U, 1);
	uchar* pixel = (uchar *)imgSrc->imageData;	
	ushort* data = (ushort *)image->imageData;
	for (int i = 0; i < rownum; i++)
	{
		if (i < rownum / 2)
		{
			m = image->width * (2 * i + 1);//注:此处使用image->width而不是image->widthStep
		}
		else
		{
			m = image->width * (2 * (i - (rownum / 2)));
		}
		int k = i*imgSrc->width;
		for (int j = 0; j < colnum; j++)
		{

			if (j % 2 == 1)
			{
				data[m + j / 2] = (pixel[ k + j - 1] << 8) | pixel[ k + j];
			}
		}
	}
	return image;
}
IplImage* Img14TO8(IplImage* imgSrc)//函数实现16bit转化为8bit功能,由于输入图像只有14bit有效,所以函数名为Img14TO8
{
	int rownum = imgSrc->height;
	int colnum = imgSrc->width;
	IplImage *image = cvCreateImage(cvSize(colnum, rownum), IPL_DEPTH_8U, 1);
	ushort* pixel = (ushort *)imgSrc->imageData;
	uchar* data = (uchar *)image->imageData;
	for (int i = 0; i < rownum; i++)
	{
		int k = i*imgSrc->width;
		int m = i*image->width;
		for (int j = 0; j < colnum; j++)
		{
			int a = pixel[k + j] >> 6;
			data[m + j] = a;
		}
	}
	return image;
}
void main()
{

		/*IplImage* imgSrc = cvLoadImage("test3.tif", 0);*/
		IplImage* imgSrc = cvLoadImage("46001.tif", 0);
		IplImage* imgSrc0 = ImgConverse(imgSrc);
		IplImage* img8 = Img14TO8(imgSrc0);
		cvNamedWindow("1", 1);
		//cvSaveImage("0405.tif",imgSrc0,0);
		cvShowImage("1", imgSrc0);
		cvNamedWindow("2", 1);
		//cvSaveImage("0405.tif",imgSrc0,0);
		cvShowImage("2", img8);
		cvWaitKey(0);//一定要有
		cvReleaseImage(&imgSrc);
		cvReleaseImage(&imgSrc0);
		cvReleaseImage(&img8);

}


//int main()
//{
//	IplImage *src = cvLoadImage("test3.tif", 0);
//	IplImage *dst;
//	CvSize size;
//	double scale = 0.5;     //缩放的倍数
//	size.width = src->width*scale;
//	size.height = src->height*scale;
//	dst = cvCreateImage(size, src->depth, src->nChannels);
//	cvResize(src, dst, CV_INTER_CUBIC);
//	cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
//	cvNamedWindow("dst", CV_WINDOW_AUTOSIZE);
//	cvShowImage("src", src);
//	cvShowImage("dst", dst);
//	cvWaitKey(0);
//	cvReleaseImage(&src);
//	cvReleaseImage(&dst);
//	cvDestroyWindow("src");
//	cvDestroyWindow("dst");
//	return 0;
//}

 

发布了101 篇原创文章 · 获赞 135 · 访问量 26万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览