Здравствуйте! Нашел в сети аналоги матлабовской функции imadjust:
a)Imadjust.cpp
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
/*
src and dst are grayscale, 8-bit images;
Default input value:
[low, high] = [0,1]; X-Direction
[bottom, top] = [0,1]; Y-Direction
gamma ;
if adjust successfully, return 0, otherwise, return non-zero.
*/
int ImageAdjust(IplImage* src, IplImage* dst,
double low, double high, // X·ЅПтЈєlow and high are the intensities of src
double bottom, double top, // Y·ЅПтЈєmapped to bottom and top of dst
double gamma )
{
if( low<0 && low>1 && high <0 && high>1&&
bottom<0 && bottom>1 && top<0 && top>1 && low>high)
return -1;
double low2 = low*255;
double high2 = high*255;
double bottom2 = bottom*255;
double top2 = top*255;
double err_in = high2 - low2;
double err_out = top2 - bottom2;
int x,y;
double val;
// intensity transform
for( y = 0; y < src->height; y++)
{
for (x = 0; x < src->width; x++)
{
val = ((uchar*)(src->imageData + src->widthStep*y))[x];
val = pow((val - low2)/err_in, gamma) * err_out + bottom2;
if(val>255) val=255; if(val<0) val=0; // Make sure src is in the range [low,high]
((uchar*)(dst->imageData + dst->widthStep*y))[x] = (uchar) val;
}
}
return 0;
}
int main( int argc, char** argv )
{
IplImage *src = 0, *dst = 0;
src = cvLoadImage("Image0",1);
if( argc != 2 || (src=cvLoadImage(argv[1], 0))) // force to gray image
return -1;
//char* filename = argv[1] : "Image0.jpg";/////////проверить///
cvNamedWindow( "src", 1 );
cvNamedWindow( "result", 1 );
// Image adjust
dst = cvCloneImage(src);
// КдИлІОКэ [0,0.5] єН [0.5,1], gamma=1
if( ImageAdjust( src, dst, 0, 0.5, 0.5, 1, 1)!=0) return -1;
cvShowImage( "src", src );
cvShowImage( "result", dst );
//cvWaitKey(0);
//cvDestroyWindow("src");
// cvDestroyWindow("result");
//cvReleaseImage( &src );
//cvReleaseImage( &dst );
return 0;
}
imageAdjustOpenCV.cpp
void
getOptimalImgAdjustParamsFromHist (IplImage* p_img,unsigned int* p_optminmaxidx, int p_count)
{
int numBins = 256;
CvMat* bins = cvCreateMat(1,numBins,CV_8UC1);
calcHistogram(p_img,bins,numBins);
int sumlow = 0, sumhigh = 0;
int low_idx = 0, high_idx = 0;
for (unsigned int i = 0; i < numBins; i++) {
float curval = (float) cvGetReal1D (bins, (i));
sumlow += curval;
if (sumlow >= p_count) {
low_idx = i;
break;
}
}
for (unsigned int i = numBins - 1 ; i >= 0; i--) {
float curval = (float) cvGetReal1D (bins, (i));
sumhigh += curval;
if (sumhigh >= p_count) {
high_idx = i;
break;
}
}
cvReleaseMat(&bins);
p_optminmaxidx[OPTMINIDX] = low_idx;
p_optminmaxidx[OPTMAXIDX] = high_idx;
}
IplImage *
imageAdjust (IplImage * p_img)
{
CvSize framesize = cvGetSize (p_img);
int low_count = round (framesize.width * framesize.height * 0.01);
unsigned int *optminmaxidx = new unsigned int [2];
getOptimalImgAdjustParamsFromHist (p_img, optminmaxidx,low_count);
int range = optminmaxidx[OPTMAXIDX] - optminmaxidx[OPTMINIDX];
IplImage *adjustedImg = p_img;
for (int i = 0; i < framesize.height; i++)
for (int j = 0; j < framesize.width; j++) {
unsigned int val = (unsigned int) getData (p_img, i, j);
unsigned int newval = 0;
if (val <= optminmaxidx[OPTMINIDX]) {
newval = 0;
setData (adjustedImg, i, j, (uchar) newval);
} else if (val >= optminmaxidx[OPTMAXIDX]) {
newval = 255;
setData (adjustedImg, i, j, (uchar) newval);
} else {
newval =
(unsigned int) round ((double) (((double) val -
(double) optminmaxidx[OPTMINIDX]) * (double) (255.0 /
(double) range)));
setData (adjustedImg, i, j, (uchar) newval);
}
}
delete[]optminmaxidx;
return adjustedImg;
}
Т.к. я только начинаю осваивать программирование, то подскажите пожалуйста:
1) почему Imadjust.cpp - нормально компилируется, без ошибок, но не работает?
2) какая из этих 2-ух функций лучше ( подходит под матлабовскую)?
3) как в проекте Visual Studio правильно включить внешнюю функцию (отдельный cpp-файл)?