请问哪位有图像处理的代码,任何代码都可以,谢谢……
发布网友
发布时间:2023-08-12 16:30
我来回答
共1个回答
热心网友
时间:2024-08-17 16:57
你要哪方面的?给你个sobel求梯度灰度吧
#include"cxcore.h"
#include"highgui.h"
#include<cmath>
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cxcore.lib")
#define Threshold 150
IplImage* src; //原图
IplImage* tar; // 边缘检测后生成的新图
IplImage* gray; //灰度图
//sobel 算子
int Func_Sobel(IplImage *src,IplImage* tar,int limit)
{
int sobelmask1[3][3], sobelmask2[3][3];
//水平模板
sobelmask1[0][0] = -1; //{-1,0,1,-2,0,2,-1,0,1};
sobelmask1[0][1] = -2;
sobelmask1[0][2] = -1;
sobelmask1[1][0] = 0;
sobelmask1[1][1] = 0;
sobelmask1[1][2] = 0;
sobelmask1[2][0] = 1;
sobelmask1[2][1] = 2;
sobelmask1[2][2] = 1;
//垂直模板
sobelmask2[0][0] = -1; //{-1,-2,-1,0,0,0,1,2,1};
sobelmask2[0][1] = 0;
sobelmask2[0][2] = 1;
sobelmask2[1][0] = -2;
sobelmask2[1][1] = 0;
sobelmask2[1][2] = 2;
sobelmask2[2][0] = -1;
sobelmask2[2][1] = 0;
sobelmask2[2][2] = 1;
int H=src->height;
int W=src->width;
int i,j,h,w,temp1,temp2;
for (h=1;h<H-1;h++)
{
for (w=1;w<W-1;w++)
{
temp1=temp2=0;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
temp1+=(src->imageData+(h-1+i)*src->widthStep)[(w-1+j)]*sobelmask1[i][j];
temp2+=(src->imageData+(h-1+i)*src->widthStep)[(w-1+j)]*sobelmask2[i][j];
}
}
double k=sqrt(temp1*temp1+temp2*temp2);
if (k>limit)
{
(tar->imageData+ tar->widthStep*h )[w]=(uchar)255; //从定值到变量转换要强制转换数据类型
}
else
(tar->imageData+tar->widthStep*h)[w]=(uchar)0;
}
}
return 0;
}
//获取灰度图
void Func_GetGrayImage(IplImage *src,IplImage *tar)
{
int H=src->height;
int W=src->width;
int w,h;
for (h=0;h<H;h++)
{
for (w=0;w<W;w++)
{
(tar->imageData+h*tar->widthStep)[w]=(unsigned char)((unsigned char)(src->imageData+h*src->widthStep)[3*w+2]*0.136+(unsigned char)(src->imageData+h*src->widthStep)[3*w+1]*0.514+(unsigned char)(src->imageData+h*src->widthStep)[3*w]*0.350);
}
}
}
void Func_ShowWindow()
{
cvNamedWindow("原图",0); //CV库函数,窗口定义函数,定义窗口,用于下一步的显示
cvShowImage("原图",src); //cv库函数,标题栏是“原图”,显示src所指向的图像
cvNamedWindow("灰度图",0);
cvShowImage("灰度图",gray);
cvNamedWindow("边缘检测图形",0);
cvShowImage("边缘检测图形",tar);
}
void Func_SaveWindow()
{
cvSaveImage("灰度图.jpg",gray);
cvSaveImage("边缘检测图.jpg",tar);
}
void Func_CloseWindow()
{
cvReleaseImage(&src);
cvReleaseImage(&gray);
cvReleaseImage(&tar);
cvDestroyWindow("原图");
cvDestroyWindow("灰度图");
cvDestroyWindow("边缘检测图");
}
void main()
{
if ( (src=cvLoadImage("test.bmp")) )
{
tar=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
gray=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
Func_GetGrayImage(src,gray);
Func_Sobel(gray,tar,Threshold); //阈值默认为150
Func_ShowWindow();
Func_SaveWindow();
cvWaitKey(100000);
Func_CloseWindow();
}
return ;
}