本文整理汇总了C++中DImage::dataPointer_u8方法的典型用法代码示例。如果您正苦于以下问题:C++ DImage::dataPointer_u8方法的具体用法?C++ DImage::dataPointer_u8怎么用?C++ DImage::dataPointer_u8使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DImage
的用法示例。
在下文中一共展示了DImage::dataPointer_u8方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: toDImage
/**The resulting image will be 8-bit grayscale and if fVertical is
* true, then the image will be numPixels wide and dataLen() high (if
* fVertical is false, then the image will be dataLen() wide and
* numPixels high). The grayscale values for the profile foreground
* and background can be specified with fg and bg, respectively. (fg
* specifies the pixels that are part of the profile, bg specifies the
* pixels that are NOT part of the profile)
*/
DImage DProfile::toDImage(int numPixels, bool fVertical,
D_uint8 fg, D_uint8 bg){
DImage img;
double max;
D_uint8 *pdata;
img.create(numPixels, len, DImage::DImage_u8);
pdata = img.dataPointer_u8();
max = 0.;
for(int i = 0; i < len; ++i){
if(rgProf[i] > max)
max = rgProf[i];
}
// printf("DProfile::toDImage() max=%f\n",max);
for(int y = 0, idx = 0; y < len; ++y){
double fillTo;
fillTo = rgProf[y] * numPixels / max;
for(int x = 0; x < numPixels; ++x, ++idx){
if(x <= fillTo)
pdata[idx] = fg;
else
pdata[idx] = bg;
}
}
if(!fVertical){
DImage imgRot;
img.rotate90_(imgRot, -90);
return imgRot;
}
return img;
}
示例2: getImageVerticalProfile
/**Each row of img is projected onto the vertical axis. Resulting
data length will be equal to the height of img. The profile is a summation
of the grayscale values in each row. If fNormalize is true, then each value
is divided by img.width() so it is the average grayscale value for the row
instead of the sum.
If fNormalize is true, the resulting profile values are divided by the image
width.
*/
void DProfile::getImageVerticalProfile(const DImage &img, bool fNormalize){
int w, h;
w = img.width();
h = img.height();
// allocate the rgProf array
if(NULL == rgProf){
rgProf = (double*)malloc(h * sizeof(double));
D_CHECKPTR(rgProf);
len = h;
}
else{
if(len != h){
rgProf = (double*)realloc(rgProf,h*sizeof(double));
D_CHECKPTR(rgProf);
len = h;
}
}
switch(img.getImageType()){
case DImage::DImage_u8:
{
D_uint8 *pu8;
pu8=img.dataPointer_u8();
for(int y = 0, idx=0; y < h; ++y){
rgProf[y] = 0.;
for(int x = 0; x < w; ++x, ++idx){
rgProf[y] += pu8[idx];
}
if(fNormalize)
rgProf[y] /= w;
}
}
break;
case DImage::DImage_flt_multi:
{
float *pflt;
if(img.numChannels() > 1){
fprintf(stderr,"DProfile::getImageVerticalProfile() floats only "
"supported with a single channel\n");
abort();
}
pflt=img.dataPointer_flt(0);
for(int y = 0, idx=0; y < h; ++y){
rgProf[y] = 0.;
for(int x = 0; x < w; ++x, ++idx){
rgProf[y] += pflt[idx];
}
if(fNormalize)
rgProf[y] /= w;
}
}
break;
default:
fprintf(stderr, "Not yet implemented!\n");
abort();
}//end switch(img.getImageType())
}
示例3: convertDImageToMat
void convertDImageToMat(DImage& dimg, Mat* mat)
{
*mat = Mat_<unsigned char>(dimg.height(),dimg.width());
unsigned char* dataD = dimg.dataPointer_u8();
unsigned char* dataM = mat->data;
for (int i=0; i< dimg.width() * dimg.height(); i++)
{
dataM[i]=dataD[i];
}
}
示例4: DImageToMat
Mat DImageToMat(const DImage& src)
{
Mat img(src.height(),src.width(),CV_8U);
unsigned char* data1 = src.dataPointer_u8();
unsigned char* data0 = img.data;
for (int i=0; i< src.height() * src.width(); i++)
{
data0[i]=data1[i];
}
return img;
}
示例5: getHorizMaxRunlengthProfile
/**Max runlength in each column of img is projected onto the horizontal axis.
Resulting data length will be equal to the width of img.
If fNormalize is true, each profile value will be divided by image height,
so the value is a fraction of the image height instead of a number of pixels.
*/
void DProfile::getHorizMaxRunlengthProfile(const DImage &img, D_uint32 rgbVal,
bool fNormalize){
int w, h;
unsigned int *rgRunlengths;
w = img.width();
h = img.height();
// allocate the rgProf array
if(NULL == rgProf){
rgProf = (double*)malloc(w * sizeof(double));
D_CHECKPTR(rgProf);
len = w;
}
else{
if(len != w){
rgProf = (double*)realloc(rgProf,w*sizeof(double));
D_CHECKPTR(rgProf);
len = w;
}
}
rgRunlengths = (unsigned int*)malloc(sizeof(unsigned int)*w);
D_CHECKPTR(rgRunlengths);
memset(rgRunlengths, 0, sizeof(unsigned int)*w);
memset(rgProf, 0, sizeof(double) * w);
switch(img.getImageType()){
case DImage::DImage_u8:
{
D_uint8 *pu8;
pu8=img.dataPointer_u8();
for(int y = 0, idx=0; y < h; ++y){
for(int x = 0; x < w; ++x, ++idx){
if((D_uint8)rgbVal == pu8[idx]){//increment run length for this col
++(rgRunlengths[x]);
if(rgRunlengths[x] > rgProf[x])
rgProf[x] = (double)rgRunlengths[x];
}
else{
rgRunlengths[x] = 0;
}
}
}
if(fNormalize){
for(int x = 0; x < w; ++x)
rgProf[x] /= h;
}
}
break;
default:
fprintf(stderr, "Not yet implemented!\n");
abort();
}//end switch(img.getImageType())
free(rgRunlengths);
}
示例6: MatToDImage
DImage MatToDImage(const Mat& src)
{
DImage img;
img.setLogicalSize(src.cols,src.rows);
unsigned char* data1 = img.dataPointer_u8();
unsigned char* data0 = src.data;
for (int i=0; i< src.cols * src.rows; i++)
{
data1[i]=data0[i];
}
return img;
}
示例7: getVertAvgRunlengthProfile
/**Avg runlength in each column of img is projected onto the vertical axis.
Resulting data length will be equal to the height of img.
If fNormalize is true, each profile value will be divided by image width,
so the value is a fraction of the image width instead of a number of pixels.
*/
void DProfile::getVertAvgRunlengthProfile(const DImage &img, D_uint32 rgbVal,
bool fNormalize){
int w, h;
unsigned int runLength, numRuns;
w = img.width();
h = img.height();
// allocate the rgProf array
if(NULL == rgProf){
rgProf = (double*)malloc(h * sizeof(double));
D_CHECKPTR(rgProf);
len = h;
}
else{
if(len != h){
rgProf = (double*)realloc(rgProf,h*sizeof(double));
D_CHECKPTR(rgProf);
len = h;
}
}
switch(img.getImageType()){
case DImage::DImage_u8:
{
D_uint8 *pu8;
pu8=img.dataPointer_u8();
for(int y = 0, idx=0; y < h; ++y){
rgProf[y] = 0.;
runLength = 0;
numRuns = 0;
for(int x = 0; x < w; ++x, ++idx){
if((D_uint8)rgbVal == pu8[idx]){//increment run length for this row
if(0==runLength)
++numRuns;
++runLength;
++(rgProf[y]);
}
else{
runLength = 0;
}
}
if(numRuns > 0)
rgProf[y] /= numRuns; //(we have sum and need to divide for avg)
if(fNormalize)
rgProf[y] /= w;
}
}
break;
default:
fprintf(stderr, "Not yet implemented!\n");
abort();
}//end switch(img.getImageType())
}
示例8: getImageHorizontalProfile
/**Each column of img is projected onto the horizontal axis.
Resulting data length will be equal to the width of img.
If fNormalize is true, the resulting profile values are divided by the image
height.
*/
void DProfile::getImageHorizontalProfile(const DImage &img, bool fNormalize){
int w, h;
w = img.width();
h = img.height();
// allocate the rgProf array
if(NULL == rgProf){
rgProf = (double*)malloc(w * sizeof(double));
D_CHECKPTR(rgProf);
len = w;
}
else{
if(len != w){
rgProf = (double*)realloc(rgProf,w*sizeof(double));
D_CHECKPTR(rgProf);
len = w;
}
}
memset(rgProf, 0, sizeof(double) * w);
switch(img.getImageType()){
case DImage::DImage_u8:
{
D_uint8 *pu8;
pu8=img.dataPointer_u8();
for(int y = 0, idx=0; y < h; ++y){
for(int x = 0; x < w; ++x, ++idx){
rgProf[x] += pu8[idx];
}
}
if(fNormalize){
for(int x = 0; x < w; ++x)
rgProf[x] /= h;
}
}
break;
default:
fprintf(stderr, "Not yet implemented!\n");
abort();
}//end switch(img.getImageType())
}
示例9: getTextlineSlantAngleDeg
/**The slant angle is assumed to be between 60 and -45 degrees (0 deg=vertical,
* negative values are left-slanted, positive values right-slanted).
* To determine slant: at each x-position, the longest runlength at each angle
* is found and its squared value is added into the accumulator for that angle.
* The histogram is smoothed, and the angle corresponding to the highest value
* in the histogram is the returned angle (in degrees).
*
* Runlengths of less than rlThresh pixels are ignored.
*
* The image should be black(0) and white(255). The portion of the image
* specified by x0,y0 - x1,y1 is considered to be the textline of interest.
* If no coordinates are specified, then the entire image is used as the
* textline.
*
* If weight is not NULL, it will be the sum of max runlengths (not squared) at
* all 120 angles. Weights are used in determination of weighted average angle
* for all textlines in getAllTextlinesSlantAngleDeg() before adjusting angles.
*
* If rgSlantHist is not NULL, the squared max RL values in the angle histogram
* will be copied into the rgSlantHist array. It must already be allocated to
* 120*sizeof(unsigned int).
*
* if imgAngleHist is not NULL, then the image is set to w=120 and h=y1-y0+1.
* It is a (grayscale) graphical representation of what is in rgSlantHist.
*/
double DSlantAngle::getTextlineSlantAngleDeg(DImage &imgBW,
int rlThresh,
int x0,int y0,int x1,int y1,
double *weight,
unsigned int *rgSlantHist,
DImage *imgAngleHist){
int *rgLineSlantAngles;
int lineH;
int slantOffset, slantAngle, angle;
unsigned int rgSlantSums[120];
unsigned int rgSlantSumsTmp[120];
int runlen, maxrl; /* maximum slant runlen */
double slantDx;
int w, h;
D_uint8 *p8;
double dblWeight = 0;
w = imgBW.width();
h = imgBW.height();
p8 = imgBW.dataPointer_u8();
if(-1 == x1)
x1 = w-1;
if(-1 == y1)
y1 = h-1;
lineH = y1-y0+1;
/* estimate the predominant slant angle (0=vertical, +right, -left) */
slantOffset = (int)(0.5+ (lineH / 2.0) / tan(DMath::degreesToRadians(30.)));
for(int j = 0; j < 120; ++j){
rgSlantSums[j] = 0;
rgSlantSumsTmp[j] = 0;
}
for(angle = -45; angle <= 60; angle += 1){
/* at each x-position, sum the maximum run length at that angle into the
accumulator */
if(0 == angle) /* vertical, so tangent is infinity */
slantDx = 0.;
else
slantDx = -1.0 / tan(DMath::degreesToRadians(90-angle));
// for(j = slantOffset; j < (hdr.w-slantOffset); ++j){
for(int j = x0; j <= x1; ++j){
maxrl = 0;
runlen = 0;
for(int y = 0; y < lineH; ++y){
int x;
x = (int)(0.5+ j + y * slantDx);
if( (x>=x0) && (x <= x1)){ /* make sure we are within bounds */
int idxtmp;
idxtmp = (y+y0)*w+x;
// imgCoded[idxtmp*3] = 0;
if(0 == p8[idxtmp]){
++runlen;
if(runlen > maxrl){
maxrl = runlen;
}
}
else
runlen = 0;
} /* end if in bounds */
else{
runlen = 0; /* ignore runs that go off edge of image */
}
}
if(maxrl > rlThresh){
rgSlantSums[angle+45] += maxrl*maxrl;
dblWeight += maxrl;
}
} /* end for j */
} /* end for angle */
//smooth the histogram
rgSlantSumsTmp[0] = (rgSlantSums[0] + rgSlantSums[1]) / 2;
for(int aa = 1; aa < 119; ++aa){
rgSlantSumsTmp[aa]=(rgSlantSums[aa-1]+rgSlantSums[aa]+rgSlantSums[aa+1])/3;
//.........这里部分代码省略.........
示例10: getSlant_thread_func
void* DSlantAngle::getSlant_thread_func(void *params){
SLANT_THREAD_PARMS *pparms;
int numThreads;
int w, h;
D_uint8 *p8;
int runlen, maxrl; /* maximum slant runlen */
double slantDx;
int lineH;
int slantOffset, slantAngle, angle;
double dblWeight;
DImage *pimg;
int rlThresh;
pparms = (SLANT_THREAD_PARMS*)params;
numThreads = pparms->numThreads;
pimg = pparms->pImgSrc;
rlThresh = pparms->rlThresh;
w = pimg->width();
h = pimg->height();
p8 = pimg->dataPointer_u8();
for(int i=0; i < 120; ++i)
pparms->rgSlantSums[i] = 0;
for(int tl=pparms->threadNum; tl < (pparms->numTextlines); tl+=numThreads){
int x0, y0, x1, y1;
unsigned int rgSlantSums[120];
x0 = pparms->rgRects[tl].x;
y0 = pparms->rgRects[tl].y;
x1 = pparms->rgRects[tl].x + pparms->rgRects[tl].w - 1;
y1 = pparms->rgRects[tl].y + pparms->rgRects[tl].h - 1;
lineH = y1-y0+1;
memset(rgSlantSums, 0, sizeof(int)*120);
dblWeight = 0.;
for(angle = -45; angle <= 60; angle += 1){
/* at each x-position, sum the maximum run length at that angle into the
accumulator */
if(0 == angle) /* vertical, so tangent is infinity */
slantDx = 0.;
else
slantDx = -1.0 / tan(DMath::degreesToRadians(90-angle));
// for(j = slantOffset; j < (hdr.w-slantOffset); ++j){
for(int j = x0; j <= x1; ++j){
maxrl = 0;
runlen = 0;
for(int y = 0; y < lineH; ++y){
int x;
x = (int)(0.5+ j + y * slantDx);
if( (x>=x0) && (x <= x1)){ /* make sure we are within bounds */
int idxtmp;
idxtmp = (y+y0)*w+x;
// imgCoded[idxtmp*3] = 0;
if(0 == p8[idxtmp]){
++runlen;
if(runlen > maxrl){
maxrl = runlen;
}
}
else
runlen = 0;
} /* end if in bounds */
else{
runlen = 0; /* ignore runs that go off edge of image */
}
}
if(maxrl > rlThresh){
rgSlantSums[angle+45] += maxrl*maxrl;
dblWeight += maxrl;
}
} /* end for j */
} /* end for angle */
for(int i=0; i < 120; ++i)
pparms->rgSlantSums[i] += rgSlantSums[i];
if(NULL != (pparms->rgWeights)){
pparms->rgWeights[tl] = dblWeight;
}
if(NULL != (pparms->rgAngles)){
// need to independently figure out the angle for this particular textline
unsigned int rgSlantSumsTmp[120];
//smooth the histogram
rgSlantSumsTmp[0] = (rgSlantSums[0] + rgSlantSums[1]) / 2;
for(int aa = 1; aa < 119; ++aa){
rgSlantSumsTmp[aa]=(rgSlantSums[aa-1]+rgSlantSums[aa]+rgSlantSums[aa+1])/3;
}
// for(int aa = 0; aa < 120; ++aa){
// rgSlantSums[aa] = rgSlantSumsTmp[aa];
// }
//use the smoothed histogram peak as the slant angle
slantAngle = 0;
for(angle = -45; angle <= 60; angle += 1){
if(rgSlantSumsTmp[angle+45] > rgSlantSumsTmp[slantAngle+45]){
slantAngle = angle;
}
} /* end for angle */
pparms->rgAngles[tl] = slantAngle;
}
}
//.........这里部分代码省略.........
示例11: memset
/** imgDst will be 2*radius pixels less wide and high than imgSrc
* because of the padding that is added before calling this function.
* This function requires that imgDst.create() has already been called
* with the proper w,h,imgType,etc.
*/
void DMaxFilter::maxFiltHuang_u8_square(DImage &imgDst,
const DImage &imgSrc,
int radiusX, int radiusY,
int wKern, int hKern,
D_uint8 *rgKern,
int numKernPxls,
DProgress *pProg,
int progStart, int progMax,
int threadNumber, int numThreads){
int rgHist[256];
int max;
unsigned char valTmp;
int idxDst;
int idx3;
D_uint8 *pTmp; // pointer to padded image data
int wTmp, hTmp; // width, height of imgSrc
int w, h; // width, height of imgDst
D_uint8 *pDst;
wTmp = imgSrc.width();
hTmp = imgSrc.height();
w = wTmp - radiusX*2;
h = hTmp - radiusY*2;
pDst = imgDst.dataPointer_u8();
pTmp = imgSrc.dataPointer_u8();
for(int y = threadNumber; y < h; y += numThreads){
// update progress report and check if user cancelled the operation
if((NULL != pProg) && (0 == (y & 0x0000003f))){
if(0 != pProg->reportStatus(progStart + y, 0, progMax)){
// the operation has been cancelled
pProg->reportStatus(-1, 0, progMax); // report cancel acknowledged
return;
}
}
// position window at the beginning of a new row and fill the kernel, hist
memset(rgHist, 0, sizeof(int)*256);
for(int kr = 0, kidx =0; kr < hKern; ++kr){
for(int kc = 0; kc < wKern; ++kc, ++kidx){
if(rgKern[kidx]){ // pixel is part of the kernel mask
++(rgHist[pTmp[(y+kr)*wTmp+kc]]);//add pixel val to histogram
}
}
}
// calculate max for first spot
for(max = 255; (max > 0) && (0==rgHist[max]); --max){
// do nothing
}
// put the max in the spot we're at
idxDst = y*w;
pDst[idxDst] = (unsigned char)max;
// remove pixels from leftmost column
idx3 = y*wTmp+radiusX;
for(int ky = 0; ky < hKern; ++ky){
valTmp = pTmp[idx3 - wKern];
--(rgHist[valTmp]);
if((valTmp==max)&&(0 == rgHist[valTmp])){//update the max
for(;(max>0)&&(0==rgHist[max]); --max){
//do nothing
}
}
idx3 += wTmp;
}
for(int x=1; x < w; ++x){
++idxDst;
// add pixels from the right-hand side of kernel (after moving over one)
idx3 = y*wTmp+x+radiusX;
for(int ky = 0; ky < hKern; ++ky){
valTmp = pTmp[idx3 + wKern];
if(valTmp > max)//update the max
max = valTmp;
++(rgHist[valTmp]);
idx3 += wTmp;
}
// put the max value in the destination pixel
pDst[idxDst] = (unsigned char)max;
// remove pixels from leftmost column for next time through loop
if(x < (w-1)){//don't need to remove left edge if going to a new row
idx3 = y*wTmp+x+radiusX;
for(int ky = 0; ky < hKern; ++ky){
valTmp = pTmp[idx3 - wKern];
--(rgHist[valTmp]);
if((valTmp==max)&&(0 == rgHist[valTmp])){//update the max
for(;(max>0)&&(0==rgHist[max]); --max){
//do nothing
}
}
idx3 += wTmp;
} // end for(ky...
//.........这里部分代码省略.........
示例12: false
//.........这里部分代码省略.........
alen = (int)(2*DMath::distPointLine(0, 0, w/2., h/2., w/2.+odx, h/2.+ody));
dblTmp = 2*DMath::distPointLine(w, 0, w/2., h/2., w/2.+odx, h/2.+ody);
if(dblTmp > alen)
alen = (int)dblTmp;
// printf("w=%d h=%d\n", w, h);
// printf("adx=%.2f ady=%.2f alen=%d\n", adx, ady, alen);
// printf("odx=%.2f ody=%.2f olen=%d\n", odx, ody, olen);
// allocate the rgProf array
if(NULL == rgProf){
rgProf = (double*)malloc(alen * sizeof(double));
D_CHECKPTR(rgProf);
len = alen;
}
else{
if(len != alen){
rgProf = (double*)realloc(rgProf,alen*sizeof(double));
D_CHECKPTR(rgProf);
len = alen;
}
}
ax = asx = w/2.-(alen/2*adx);
ay = asy = h/2.-(alen/2*ady);
osx = -(olen/2*odx);
osy = -(olen/2*ody);
switch(img.getImageType()){
case DImage::DImage_u8:
{
D_uint8 *pu8;
pu8=img.dataPointer_u8();
if(fInterp){
fprintf(stderr, "WARNING! DProfile::getImageProfile() may be buggy "
"when fInterp is true\n");
for(int aa=0; aa < alen; ++aa){
int ix, iy;
double w1, w2; //weight of left vs right pixels
double w3, w4;//weight of top vs bottom pixels
double left, right;
int oidx;
ox = ax+osx;
oy = ay+osy;
numPixels = 0;
dblSum = 0.;
for(int oo=0; oo < olen; ++oo){
if( (ox>=0) && (ox < wm1) && (oy>=0) && (oy<hm1)){
ix = (int)ox;
iy = (int)oy;
oidx = iy*w + ix;
w2 = ox-ix;
w1 = 1.-w2;
w4 = oy-iy;
w3 = 1.-w4;
left = (pu8[oidx] * w3 + pu8[oidx+w] * w4);
right = (pu8[oidx+1] * w3 + pu8[oidx+w+1] * w4);
dblSum += left*w1 + right*w2;
++numPixels;
}
else if((ox==wm1) || (oy == hm1)){
// don't interpolate on the edges, just approximate
ix = (int)ox;
iy = (int)oy;
oidx = iy*w + ix;
示例13: getAngledVertProfile
/**This function calculates the vertical profile (the length of
* profile will be the same as the height of the image). However,
* instead of projecting pixels straight across to the vertical axis,
* the projection is taken using lines angled through the middle
* (x=width/2) of the image. Linear interpolation is used for
* sampling the image pixel values since the y-position on the
* projection lines will normally be "between" two pixels for any
* given x-position. The function is intended only for angles between
* -45 and 45 degrees, since otherwise the slope will be too steep for
* the assumption I am making (I am steping through the x-values and
* calculating the y-values. If the slope is steeper, I should do the
* opposite. As a "to-do," maybe we should just check the angle and
* handle the two cases individually). */
void DProfile::getAngledVertProfile(const DImage &img, double ang,
int fNormalize){
double m;
int x;
double y, val;
int hm1;
double xc;
int numPixels;
D_uint8 *pimg;
int w, h;
int initialOffset=0;
int *rgYoffsets;
double *rgBotWeights;
int yTop;
int yTopPrev=0;
int imglen;
if(DImage::DImage_u8 != img.getImageType()){
fprintf(stderr,
"DProfile::getAngledVertProfile() currently only supports 8-bit "
"grayscale images\n");
abort();
}
w=img.width();
h=img.height();
pimg = img.dataPointer_u8();
if(NULL == rgProf){
rgProf = (double*)malloc(h * sizeof(double));
D_CHECKPTR(rgProf);
len = h;
}
else{
if(len != h){
rgProf = (double*)realloc(rgProf,h*sizeof(double));
D_CHECKPTR(rgProf);
len = h;
}
}
// memset(rgProf, 0, sizeof(double) * h);
rgYoffsets = (int*)malloc(sizeof(int)*w);
D_CHECKPTR(rgYoffsets);
rgBotWeights = (double*)malloc(sizeof(double)*w);
D_CHECKPTR(rgBotWeights);
m = 1 * tan(DMath::degreesToRadians(ang)); /* dy per dx=1 */
xc = w / 2.;
// initialOffset is the y-offset of first pixel, rgYoffsets[i] for i=1...w-1
// are the relative offsets from rgYoffsets[i-1] of the top pixel
// rgBotWeights[i] is the interpolation weight of the bottom pixel, while
// 1.-rgBotWeights[i] is the interpolation weight of the top pixel.
for(x = 0; x < w; ++x){
y = m * ((double)x - xc);
yTop = (int)(floor(y));
rgBotWeights[x] = y - (double)(yTop);
if(0 == x){
rgYoffsets[0] = 0;
initialOffset = yTop;
}
else
rgYoffsets[x] = (yTop - yTopPrev);
yTopPrev = yTop;
}
hm1 = h - 1;
imglen = w * hm1;
for(int i = 0; i < h; ++i){ /* profile index */
int idx;
rgProf[i] = 0.;
numPixels = 0;
idx = w * (i + initialOffset);
for(x = 0; x < w; ++x, ++idx){
idx += (w*rgYoffsets[x]);// go to next row if needed
if((idx <0) || (idx >= imglen)) // out of bounds
continue;
++numPixels;
val = rgBotWeights[x] * pimg[idx] + (1.-rgBotWeights[x]) * pimg[idx+w];
rgProf[i] += val;
}
if((numPixels > 0) && fNormalize)
rgProf[i] /= numPixels;
}
free(rgYoffsets);
//.........这里部分代码省略.........
示例14: getTextlineRects
/** Takes profiles of numStrips vertical strips (plus numStrips-1
overlapping strips) and uses them to estimate the avg textline
height **/
void DTextlineSeparator::getTextlineRects(DImage &img, int *numTextlines,
DRect **rgTextlineRects,
int *spacingEst,
char *stDebugBaseName){
int w, h;
D_uint8 *pu8;
DProfile prof;
DProfile profSmear;// profile of smeared image
char stTmp[1024];
w = img.width();
h = img.height();
pu8 = img.dataPointer_u8();
for(int y=0, idx=0; y < h; ++y){
for(int x=0; x < w; ++x, ++idx){
if((pu8[idx] > 0) && (pu8[idx] < 255)){
fprintf(stderr, "DTextlineSeparator::getTextlineRects() requires "
"BINARY image with values of 0 or 255!\n");
exit(1);
}
}
}
DProfile profWeightedStrokeDist;
int *rgBlackSpacingHist;
rgBlackSpacingHist=new int[200];
D_CHECKPTR(rgBlackSpacingHist);
memset(rgBlackSpacingHist,0,sizeof(int)*200);
int *rgPeakYs;
int numPeaks;
int *rgValleyYs;
int numValleys;
rgPeakYs = new int[h];
D_CHECKPTR(rgPeakYs);
rgValleyYs = new int[h];
D_CHECKPTR(rgValleyYs);
numPeaks = 0;
numValleys = 0;
{
prof.getImageVerticalProfile(img,false);
prof.smoothAvg(2);
double *pdbl;
pdbl = prof.dataPointer();
for(int j=0; j < h; ++j)
pdbl[j] /= 255; // now the profile is number of white pixels (was GS prof)
unsigned int profMax;
profMax = (unsigned int)prof.max();
//use original image to create histogram of horizontal foreground spacing
//(distance from black pixel to next black pixel) weighted by profile value
//inverse (number of black pixels instead of white pixels)
for(int y=2; y < (h-2); ++y){//ignore 2 on each end (smoothing boundaries)
int lastBlackX;
int runlength;
int x;
int weight;
x=0;
lastBlackX = -1;
runlength = 0;
for(x=0 ; x < w; ++x){
if(pu8[y*w+x] == 0){//black
runlength = x - lastBlackX;
if((runlength >= 2) && (runlength < 200)){
weight = (int)profMax - (int)pdbl[y];//inverse of profile value at y
rgBlackSpacingHist[runlength] += weight;
}
lastBlackX=x;
}
}
}
}
//to get the spacing estimate, get the max, then find the next position
//that is less than 1/3 of the max. Use that as the estimate to determine
//scale
int spacingMax;
int spacingEstimate;
spacingMax = 2;
for(int j=3; j<200; ++j){
if(rgBlackSpacingHist[j] > rgBlackSpacingHist[spacingMax])
spacingMax = j;
}
spacingEstimate = spacingMax;
for(int j=spacingMax+1; j < 200; ++j){
if(rgBlackSpacingHist[j] < (rgBlackSpacingHist[spacingMax] / 3)){
spacingEstimate = j;
break;
}
//.........这里部分代码省略.........
示例15: if
/** Takes profiles of numStrips vertical strips (plus numStrips-1
overlapping strips) and uses them to estimate the avg textline
height **/
int DTextlineSeparator::estimateAvgHeight2(DImage &imgBinary,
int numStrips,
char *stDebugBaseName){
int w, h;
D_uint8 *pu8;
DProfile prof;
DProfile *rgProfs;// profiles of overlapping strips of image
DProfile *rgProfsRL;//avg white RL profile
DProfile *rgProfsSmear;// profiles of overlapping strips of image after smear
char stTmp[1024];
int *rgPeakThresh;
int *rgPeakThreshRL;
double *rgPeakLineOffs;
rgProfs = new DProfile[numStrips*2-1];
D_CHECKPTR(rgProfs);
rgProfsRL = new DProfile[numStrips*2-1];
D_CHECKPTR(rgProfsRL);
rgProfsSmear = new DProfile[numStrips*2-1];
D_CHECKPTR(rgProfsSmear);
rgPeakThresh = new int[numStrips*2-1];
D_CHECKPTR(rgPeakThresh);
rgPeakThreshRL = new int[numStrips*2-1];
D_CHECKPTR(rgPeakThreshRL);
rgPeakLineOffs = new double[numStrips*2-1];
D_CHECKPTR(rgPeakLineOffs);
w = imgBinary.width();
h = imgBinary.height();
pu8 = imgBinary.dataPointer_u8();
for(int y=0, idx=0; y < h; ++y){
for(int x=0; x < w; ++x, ++idx){
if((pu8[idx] > 0) && (pu8[idx] < 255)){
fprintf(stderr, "DTextlineSeparator::estimateAvgHeight() requires "
"BINARY image with values of 0 or 255!\n");
exit(1);
}
}
}
DImage imgStrip;
int stripW, stripLeft;
DProfile profWeightedStrokeDist;
int **rgBlackSpacingHist;
rgBlackSpacingHist = new int*[numStrips*2-1];
D_CHECKPTR(rgBlackSpacingHist);
rgBlackSpacingHist[0]=new int[200*(numStrips*2-1)];
D_CHECKPTR(rgBlackSpacingHist[0]);
memset(rgBlackSpacingHist[0],0,sizeof(int)*200*(numStrips*2-1));
for(int i=1; i < (numStrips*2-1); ++i){
rgBlackSpacingHist[i] = &(rgBlackSpacingHist[i-1][200]);//only 2-199 are valid spacings
}
int **rgPeakYs;
int *rgNumPeaks;
int **rgValleyYs;
int *rgNumValleys;
rgPeakYs = new int*[numStrips*2-1];
D_CHECKPTR(rgPeakYs);
rgPeakYs[0] = new int[(numStrips*2-1)*h];
D_CHECKPTR(rgPeakYs[0]);
rgValleyYs = new int*[numStrips*2-1];
D_CHECKPTR(rgValleyYs);
rgValleyYs[0] = new int[(numStrips*2-1)*h];
D_CHECKPTR(rgValleyYs);
for(int i = 1; i < (numStrips*2-1); ++i){
rgPeakYs[i] = &(rgPeakYs[i-1][h]);
rgValleyYs[i] = &(rgValleyYs[i-1][h]);
}
rgNumPeaks = new int[numStrips*2-1];
D_CHECKPTR(rgNumPeaks);
rgNumValleys = new int[numStrips*2-1];
D_CHECKPTR(rgNumValleys);
for(int i=0; i < (numStrips*2-1); ++i){
rgNumPeaks[i] = 0;
rgNumValleys[i] = 0;
}
stripW = (w + numStrips-1) / numStrips;
printf("w=%d h=%d stripW=%d\n",w,h,stripW);
for(int i=0; i < numStrips*2-1; ++i){
stripLeft = i * stripW/2;
if(i == numStrips*2-2){//last strip may have slightly different width
stripW = w - stripLeft - 1;
}
imgBinary.copy_(imgStrip, stripLeft, 0, stripW, h);
rgProfs[i].getImageVerticalProfile(imgStrip,false);
rgProfs[i].smoothAvg(2);
rgProfsRL[i].getVertAvgRunlengthProfile(imgStrip,0xff,true);
rgProfsRL[i].smoothAvg(2);
double *pdbl;
pdbl = rgProfs[i].dataPointer();
//.........这里部分代码省略.........