OpenCV中如何只对IplImage图像的数据进行处理
来源:互联网 发布:java服务器端部署 编辑:程序博客网 时间:2024/06/10 02:26
//此程序的功能是实现对IplImage的imageData进行操作
//将src的数据拷贝给dst
#include "StdAfx.h"
#include <iostream.h>
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int Otsu(uchar* src,uchar* dst,const int src_width,const int src_height);
int main(int argc, char* argv[])
{
const char* filename = argc >= 2 ? argv[1] : "cir1.bmp";
IplImage* src = cvLoadImage( filename, 0 );
IplImage* dst = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
cvNamedWindow( "Resource", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Dst", CV_WINDOW_AUTOSIZE );
int height = src->height;
int width = src->width;
int Th = 0;
//********************
uchar* pSrc=NULL;
uchar* pDst=NULL;
pSrc=(uchar*)src->imageData;
pDst=(uchar*)dst->imageData;
Th = Otsu(pSrc,pDst,width,height);
printf("Th=%d\n",Th);
//********************
dst->imageData=(char*)pDst;
cvShowImage("Resource",src);
cvShowImage("Dst",dst);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvDestroyWindow("Resource");
cvDestroyWindow("Dst");
return 0;
}
int Otsu(uchar* src,uchar* dst,const int src_width,const int src_height)
{
int step = 0;
int i = 0,j = 0;
int width = src_height,height = src_width;
long int N = height * width;
int T = 0;
int h[256] = {0};
double p[256] = {0};
double u[256] = {0};
double w[256] = {0};
double uT = 0.0,sigma2fang = 0.0;
double sigma2fang_max = -10000;
if(NULL == src || 0 == src_width || 0 == src_height)
{
return -1;
}
cout<<"Otsu function begin!"<<endl;
cout<<"src_width="<<src_width<<" src_height="<<src_height<<endl;
if(0 == width%4)
{
step=width/sizeof(uchar);
}
else
{
step=(width- width%4 + 4)/sizeof(uchar);
}
cout<<"src_step="<<step<<endl;
/*
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
*(dst + step*i+j)= *(src + step*i+j);
}
}*/
//calculate histogram
for(i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
for(int k = 0; k < 256; k++)
{
if(((uchar*)(src + step*i))[j] == k)
h[k]++;
}
}
}
//calculate ratio of every bin
for(i = 0; i < 256; i++)
{
p[i] = h[i] / double(N);
}
for(int k = 0; k < 256; k++)
{
uT = 0;
for(i = 0; i <= k; i++)
{
w[k] += p[i];
u[k] += i*p[i];
}
for(i = 0; i < 256; i++)
{
uT += i*p[i];
}
sigma2fang = (uT*w[k] - u[k])*(uT*w[k] - u[k]) / (w[k]*(1-w[k]));
if(sigma2fang > sigma2fang_max)
{
sigma2fang_max = sigma2fang;
T = k;
}
}
for(i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
if(((uchar*)(src + step*i))[j] > T)
{
((uchar*)(dst + step*i))[j] = 255;
}
else
{
((uchar*)(dst + step*i))[j] = 0;
}
}
}
return T;
}
//将src的数据拷贝给dst
#include "StdAfx.h"
#include <iostream.h>
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int Otsu(uchar* src,uchar* dst,const int src_width,const int src_height);
int main(int argc, char* argv[])
{
const char* filename = argc >= 2 ? argv[1] : "cir1.bmp";
IplImage* src = cvLoadImage( filename, 0 );
IplImage* dst = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
cvNamedWindow( "Resource", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Dst", CV_WINDOW_AUTOSIZE );
int height = src->height;
int width = src->width;
int Th = 0;
//********************
uchar* pSrc=NULL;
uchar* pDst=NULL;
pSrc=(uchar*)src->imageData;
pDst=(uchar*)dst->imageData;
Th = Otsu(pSrc,pDst,width,height);
printf("Th=%d\n",Th);
//********************
dst->imageData=(char*)pDst;
cvShowImage("Resource",src);
cvShowImage("Dst",dst);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvDestroyWindow("Resource");
cvDestroyWindow("Dst");
return 0;
}
int Otsu(uchar* src,uchar* dst,const int src_width,const int src_height)
{
int step = 0;
int i = 0,j = 0;
int width = src_height,height = src_width;
long int N = height * width;
int T = 0;
int h[256] = {0};
double p[256] = {0};
double u[256] = {0};
double w[256] = {0};
double uT = 0.0,sigma2fang = 0.0;
double sigma2fang_max = -10000;
if(NULL == src || 0 == src_width || 0 == src_height)
{
return -1;
}
cout<<"Otsu function begin!"<<endl;
cout<<"src_width="<<src_width<<" src_height="<<src_height<<endl;
if(0 == width%4)
{
step=width/sizeof(uchar);
}
else
{
step=(width- width%4 + 4)/sizeof(uchar);
}
cout<<"src_step="<<step<<endl;
/*
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
*(dst + step*i+j)= *(src + step*i+j);
}
}*/
//calculate histogram
for(i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
for(int k = 0; k < 256; k++)
{
if(((uchar*)(src + step*i))[j] == k)
h[k]++;
}
}
}
//calculate ratio of every bin
for(i = 0; i < 256; i++)
{
p[i] = h[i] / double(N);
}
for(int k = 0; k < 256; k++)
{
uT = 0;
for(i = 0; i <= k; i++)
{
w[k] += p[i];
u[k] += i*p[i];
}
for(i = 0; i < 256; i++)
{
uT += i*p[i];
}
sigma2fang = (uT*w[k] - u[k])*(uT*w[k] - u[k]) / (w[k]*(1-w[k]));
if(sigma2fang > sigma2fang_max)
{
sigma2fang_max = sigma2fang;
T = k;
}
}
for(i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
if(((uchar*)(src + step*i))[j] > T)
{
((uchar*)(dst + step*i))[j] = 255;
}
else
{
((uchar*)(dst + step*i))[j] = 0;
}
}
}
return T;
}
- OpenCV中如何只对IplImage图像的数据进行处理
- OpenCV对IplImage类中图像深度的理解
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与 BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与 BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCv中IplImage图像格式与BYTE图像数据的转换
- OPENcv从相机采集图像数据-图像处理接口IPLIMAGE结构的指针
- OPENcv从相机采集图像数据-图像处理接口IPLIMAGE结构的指针
- OpenCV用GDI或者GDI+函数对IplImage图像处理
- openCV--图像处理类IplImage
- 谈谈我对opencv中IplImage图像参数widthstep的理解
- opencv中如何访问IplImage图像中每个像素的值
- OpenCV中对图像数据进行64F和8U转换的方法
- 【OpenCV图像处理】IplImage转换灰度图像及Cvmat与IplImage格式之间的相互转换
- asp.net利用Ajax和Jquery在前台向后台传参数并返回值
- IOS 5中的strong和weak关键字
- 快速排序quicksort
- Machine Learning week 5 Neural Networks Learning
- chrome 快捷键
- OpenCV中如何只对IplImage图像的数据进行处理
- erlang杂记六 --- 写了个小函数(2):最长子序列
- 希尔排序 shell sort
- zedGraph拖动打开文件
- 第十一次课后作业
- Session同步
- Android五个进程等级
- 强制类型转换-值和地址
- JavaScript语言的使用