Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ 如何使用opencv直方图确定图像块的平均值_Visual C++_Opencv - Fatal编程技术网

Visual c++ 如何使用opencv直方图确定图像块的平均值

Visual c++ 如何使用opencv直方图确定图像块的平均值,visual-c++,opencv,Visual C++,Opencv,我想使用直方图确定图像的平均块。假设我的图像有64×64维,我需要将其划分为4×4块,然后确定每个块的平均值(换句话说,现在我将有4块) 使用opencv,如何利用我的IplImage使用直方图箱确定块平均值 下面的代码是opencv直方图,用于确定整个图像的平均值: int i, hist_size = 256; float max_value,min_value; float min_idx,max_idx; float bin_w; float mean =0, low_mean =0,

我想使用直方图确定图像的平均块。假设我的图像有64×64维,我需要将其划分为4×4块,然后确定每个块的平均值(换句话说,现在我将有4块)

使用opencv,如何利用我的IplImage使用直方图箱确定块平均值

下面的代码是opencv直方图,用于确定整个图像的平均值:

int i, hist_size = 256;
float max_value,min_value;
float min_idx,max_idx;
float bin_w;
float mean =0, low_mean =0, high_mean =0, variance =0;

float range_0[]={0,256};
float *ranges[]={range_0};

IplImage* im = cvLoadImage("killerbee.jpg");
//Create a single planed image of the same size as the original
IplImage* grayImage = cvCreateImage(cvSize(im->width,im->height),IPL_DEPTH_8U, 1);
//convert the original image to gray
cvCvtColor(im, grayImage, CV_BGR2GRAY);

/* Remark this, since wanna evaluate whole area.
    //create a rectangular area to evaluate
    CvRect rect = cvRect(0, 0, 500, 600 );
    //apply the rectangle to the image and establish a region of interest
    cvSetImageROI(grayImage, rect);
End remark*/

//create an image to hold the histogram
IplImage* histImage = cvCreateImage(cvSize(320,200), 8, 1);
//create a histogram to store the information from the image
CvHistogram* hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
//calculate the histogram and apply to hist
cvCalcHist( &grayImage, hist, 0, NULL );

//grab the min and max values and their indeces
cvGetMinMaxHistValue( hist, &min_value, &max_value, 0, 0);
//scale the bin values so that they will fit in the image representation
cvScale( hist->bins, hist->bins, ((double)histImage->height)/max_value, 0 );

//set all histogram values to 255
cvSet( histImage, cvScalarAll(255), 0 );
//create a factor for scaling along the width
bin_w = cvRound((double)histImage->width/hist_size);

for( i = 0; i < hist_size; i++ ) {
    //draw the histogram data onto the histogram image
    cvRectangle( histImage, cvPoint(i*bin_w, histImage->height),cvPoint((i+1)*bin_w,histImage->height - cvRound(cvGetReal1D(hist->bins,i))),cvScalarAll(0), -1, 8, 0 );
    //get the value at the current histogram bucket
    float* bins = cvGetHistValue_1D(hist,i);
    //increment the mean value
    mean += bins[0];
}
//finish mean calculation
mean /= hist_size;

//display mean value onto output window
cout<<"MEAN VALUE of THIS IMAGE : "<<mean<<"\n";

//go back through now that mean has been calculated in order to calculate variance
for( i = 0; i < hist_size; i++ ) {
    float* bins = cvGetHistValue_1D(hist,i);
    variance += pow((bins[0] - mean),2);
}
//finish variance calculation
variance /= hist_size;
cvNamedWindow("Original", 0);
cvShowImage("Original", im );

cvNamedWindow("Gray", 0);
cvShowImage("Gray", grayImage );

cvNamedWindow("Histogram", 0);
cvShowImage("Histogram", histImage );

//hold the images until a key is pressed
cvWaitKey(0);

//clean up images
cvReleaseImage(&histImage);
cvReleaseImage(&grayImage);
cvReleaseImage(&im);

//remove windows
cvDestroyWindow("Original");
cvDestroyWindow("Gray");
cvDestroyWindow("Histogram");
inti,历史大小=256;
浮动最大值、最小值;
浮动最小idx,最大idx;
浮箱;
浮动平均值=0,低平均值=0,高平均值=0,方差=0;
浮动范围_0[]={0256};
浮点*范围[]={range_0};
IplImage*im=cvLoadImage(“killerbee.jpg”);
//创建与原始图像大小相同的单个平面图像
IplImage*grayImage=cvCreateImage(cvSize(im->width,im->height),IPL_DEPTH_8U,1);
//将原始图像转换为灰色
CVT颜色(im、灰度图像、CV_bgr2灰度);
/*注意这一点,因为我想评估整个区域。
//创建要计算的矩形区域
CvRect-rect=CvRect(0,0500600);
//将矩形应用于图像并建立感兴趣的区域
cvSetImageROI(灰度图像,矩形);
结束语*/
//创建一个图像来保存直方图
IplImage*histImage=cvCreateImage(cvSize(320200),8,1);
//创建直方图以存储图像中的信息
CvHistogram*hist=cvCreateHist(1,&hist_大小,CV_历史_数组,范围,1);
//计算直方图并应用于hist
cvCalcHist(&grayImage,hist,0,NULL);
//获取最小值和最大值及其索引
cvGetMinMaxHistValue(历史值、最小值和最大值,0,0);
//缩放仓位值,使其适合图像表示
cvScale(历史->存储箱,历史->存储箱,((双)历史图像->高度)/max_值,0);
//将所有直方图值设置为255
cvSet(histImage,cvScalarAll(255),0);
//创建沿宽度缩放的因子
bin_w=cvRound((双)历史图像->宽度/历史大小);
对于(i=0;iheight),cvPoint((i+1)*bin\u w,histImage->height-cvRound(cvGetReal1D(hist->bin,i))),cvScalarAll(0),-1,8,0);
//获取当前直方图存储桶的值
float*bins=cvGetHistValue_1D(hist,i);
//增加平均值
平均值+=箱[0];
}
//完成平均值计算
平均值/=历史尺寸;
//在输出窗口上显示平均值

cout你可以通过直方图来实现,但更有效的方法是一个整体图像,它几乎可以实现你想要的

阅读此处,然后使用它计算每个块中所有像素的总和。然后除以每个块中的像素数(4x4=16)。这不是很好吗

OpenCV有一个计算整型图像的函数,很难命名为cv::integral()

更简单的方法是谦逊的resize()

调用resize(image64_64,image_16_16,Size(16,16),INTER_AREA),结果将是一个更小的图像,其像素值与您要查找的值完全相同。这不是很棒吗


只是不要忘记INTER_区域标志。它决定了要使用的正确算法。

感谢您的回答和见解,因为我还需要直方图以供进一步使用,因此我仍然更喜欢使用直方图。有没有想过继续使用直方图来确定块平均值?