VC++6.0结合openCV1.0进行图像处理(C语言实现)

  • Post author:
  • Post category:其他




使用VC++6.0和openCV1.0进行图像处理(C语言实现)



一、 环境配置

1、工程中用到的opencv1.0库有:

highgui.lib, cv.lib, cvaux.lib, cxcore.lib, ml.lib

,打开项目–>设置,如下图将以上五个库添加到工程。

在这里插入图片描述

2、导入文件(工具–>选项–>目录)。

1)将

CV\INCLUDE,CVAUX\INCLUDE,CXCORE\INCLUDE,ML\INCLUDE,OTHERLIBS\HIGHGUI,OTHERLIBS\CVCAM\INCLUDE

文件放在include files目录中;

2)将

CV\SRC,CVAUX\SRC,CXCORE\SRC,OTHERLIBS\HIGHGUI,OTHERLIBS\CVCAM\SRC\WINDOWS

文件放在source files目录(见下图)。

在这里插入图片描述
!
在这里插入图片描述



二、图像处理代码及效果

1、读取彩色图片三通道信息,利用获取的三通道灰度值进行处理:

#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include<Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main(int argc, char** argv){	
	const char* imagFile = "test.jpg";  //test.jpg
    IplImage *image = cvLoadImage(imagFile,1); //读取图片
	struct CvSize{
		int width; 
		int height; 
	};
	//获取图片尺寸
	CvSize cvSize= cvGetSize(image);
	const int height = cvSize.height;
	const int width = cvSize.width;
	
	//读取图像数据
	uchar* data = (uchar*)image->imageData;
	int step = image->widthStep/sizeof(uchar);
	int channels = image->nChannels;
	
	//提取RGB通道对应灰度值	
	for(i = 0; i < image->height; i++){
		for(j = 0; j < image->width; j++){
			bVal[i][j] = data[i*step+j*channels+0];// B  
			gVal[i][j] = data[i*step+j*channels+1];// G
			rVal[i][j] = data[i*step+j*channels+2];// R
		}
	}

2、对模糊图像使用双线性插值算法进行分区域灰度补偿,主要代码如下:

	for(n = 0; n < 8; n++){
		for(i = 0; i < image->height; i += num){
			for(j = 0; j < image->width; j += num){
				point1 = sampleModel[n][i][j];
				point2 = sampleModel[n][i + num][j];
				point3 = sampleModel[n][i][j + num];
				point4 = sampleModel[n][i + num][j + num];
				x1 = i; y1 = j;x2 = i + num; y2 = j;
				x3 = i; y3 = j + num;x4 = i + num; y4 = j + num;
				for(k = i; k < (i + num); k++){
					for(l = j; l < (j + num); ll++){
						interploationModel[n][k][l]=((((y3 - j)*point1 + (j - y1)* point3)/num) * (x2 - i) +
						(((y4 - j)*point2 + (j - y2)* point4)/num) * (i - x1))/num;//双线性插值
						if((k == i)&&(l == j)){
							interploationModel[n][k][l] = point1;
						}
						if((k == i)&&(l == (j+num))){
							interploationModel[n][k][l] = point2;
						}
						if((k == (i+num))&&(l == j)){
							interploationModel[n][k][l] = point3;
						}
						if((k == (i+num))&&(l == (j+num))){
							interploationModel[n][k][l] = point4;
						}
					}	
				}
			}
		}
		if(n >= 1){		
			for(x = 0; x < image->height; x++){
				for(y = 0; y < image->width; y++){
					z1 = bVal[x][y];
					alpha1 = (z1 - (z1 / num)*num)/( num * (z1 / num + 1) - (z1 / num)*num); 
					z2 = gVal[x][y];
					alpha2 = (z2 - (z2 / num)*num)/( num * (z2 / num + 1) - (z2 / num)*num);  
					z3 = rVal[x][y];
					alpha3 = (z3 - (z3 / num)*num)/( num * (z3 / num + 1) - (z3 / num)*num);  
					finalModelb[n-1][x][y] = (int)((interploationModel[n-1][x][y] - interploationModel[n][x][y]) * alpha1 + interploationModel[n][x][y]);
					finalModelg[nn-1][x][y] = (int)((interploationModel[n-1][x][y] - interploationModel[n][x][y]) * alpha2 + interploationModel[n][x][y]);	
					finalModelr[n-1][x][y] = (int)((interploationModel[n-1][x][y] - interploationModel[n][x][y]) * alpha3 + interploationModel[n][x][y]);	
				}
			}			
		}	
	}

//补偿后的灰度值处理
for(a = 0; a < image->height; a++){
		for(b = 0; b < image->width; b++){
			bVal2[a][b] = bVal[a][b] + finalModelb[random1][a][b];
			gVal2[a][b] = gVal[a][b] + finalModelg[random2][a][b];
			rVal2[a][b] = rVal[a][b] + finalModelr[random3][a][b];
			if((bVal2[a][b]>255)){
				bVal2[a][b] = bVal[a][b];	
			}
			if((gVal2[a][b]>255)){
				gVal2[a][b] = gVal[a][b];	
			}
			if((rVal2[a][b]>255)){
				rVal2[a][b] = rVal[a][b];	
			}			
		}
	}

3、将处理后的图像映射到图像空间中,代码和效果如下:

//定义图像空间
IplImage* processedImage;
for(i = 0; i < image->height; i++){
		for(j = 0; j < image->width; j++){
			processedImage->imageData[i*processedImage->widthStep+j*3+0] = (uchar)(bVal2[i1][j1]);
			processedImage->imageData[i*processedImage->widthStep+j*3+1] = (uchar)(gVal2[i][j]);
			processedImage->imageData[i*processedImage->widthStep+j*3+2] = (uchar)(rVal2[i][j]);
		}
	}
//图像输出和保存
	cvNamedWindow("processedImage",1);  
    cvShowImage("processedImage",processedImage);
	cvSaveImage("result1.jpg",processedImage);
    cvWaitKey(0);  

//释放内存
	cvDestroyWindow( "processedImage" );
    cvReleaseImage( &processedImage );

处理效果如下:

处理前:


处理后:



版权声明:本文为zwl2220943286原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。