本文整理汇总了C++中pixInvert函数的典型用法代码示例。如果您正苦于以下问题:C++ pixInvert函数的具体用法?C++ pixInvert怎么用?C++ pixInvert使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pixInvert函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RemoveEnclosingCircle
// Helper to remove an enclosing circle from an image.
// If there isn't one, then the image will most likely get badly mangled.
// The returned pix must be pixDestroyed after use. NULL may be returned
// if the image doesn't meet the trivial conditions that it uses to determine
// success.
static Pix* RemoveEnclosingCircle(Pix* pixs) {
Pix* pixsi = pixInvert(NULL, pixs);
Pix* pixc = pixCreateTemplate(pixs);
pixSetOrClearBorder(pixc, 1, 1, 1, 1, PIX_SET);
pixSeedfillBinary(pixc, pixc, pixsi, 4);
pixInvert(pixc, pixc);
pixDestroy(&pixsi);
Pix* pixt = pixAnd(NULL, pixs, pixc);
l_int32 max_count;
pixCountConnComp(pixt, 8, &max_count);
// The count has to go up before we start looking for the minimum.
l_int32 min_count = MAX_INT32;
Pix* pixout = NULL;
for (int i = 1; i < kMaxCircleErosions; i++) {
pixDestroy(&pixt);
pixErodeBrick(pixc, pixc, 3, 3);
pixt = pixAnd(NULL, pixs, pixc);
l_int32 count;
pixCountConnComp(pixt, 8, &count);
if (i == 1 || count > max_count) {
max_count = count;
min_count = count;
} else if (i > 1 && count < min_count) {
min_count = count;
pixDestroy(&pixout);
pixout = pixCopy(NULL, pixt); // Save the best.
} else if (count >= min_count) {
break; // We have passed by the best.
}
}
pixDestroy(&pixt);
pixDestroy(&pixc);
return pixout;
}
示例2: main
main(int argc,
char **argv)
{
l_int32 i, w, h, d;
l_float32 time;
PIX *pixs, *pixf, *pixd;
PIXA *pixa;
char *filein, *fileout;
static char mainName[] = "edgetest";
if (argc != 3)
exit(ERROR_INT(" Syntax: edgetest filein fileout", mainName, 1));
filein = argv[1];
fileout = argv[2];
if ((pixs = pixRead(filein)) == NULL)
exit(ERROR_INT("pix not made", mainName, 1));
pixGetDimensions(pixs, &w, &h, &d);
if (d != 8)
exit(ERROR_INT("pix not 8 bpp", mainName, 1));
/* Speed: about 12 Mpix/GHz/sec */
startTimer();
pixf = pixSobelEdgeFilter(pixs, L_HORIZONTAL_EDGES);
pixd = pixThresholdToBinary(pixf, 60);
pixInvert(pixd, pixd);
time = stopTimer();
fprintf(stderr, "Time = %7.3f sec\n", time);
fprintf(stderr, "MPix/sec: %7.3f\n", 0.000001 * w * h / time);
pixDisplay(pixs, 0, 0);
pixInvert(pixf, pixf);
pixDisplay(pixf, 480, 0);
pixDisplay(pixd, 960, 0);
pixWrite(fileout, pixf, IFF_PNG);
pixDestroy(&pixd);
/* Threshold at different values */
pixInvert(pixf, pixf);
for (i = 10; i <= 120; i += 10) {
pixd = pixThresholdToBinary(pixf, i);
pixInvert(pixd, pixd);
pixDisplayWrite(pixd, 1);
pixDestroy(&pixd);
}
pixDestroy(&pixf);
/* Display tiled */
pixa = pixaReadFiles("/tmp", "junk_write_display");
pixd = pixaDisplayTiledAndScaled(pixa, 8, 400, 3, 0, 25, 2);
pixWrite("/tmp/junktiles.jpg", pixd, IFF_JFIF_JPEG);
pixDestroy(&pixd);
pixaDestroy(&pixa);
pixDestroy(&pixs);
exit(0);
}
示例3: GetBinaryImage
/**
* Returns an image of the current object at the given level in greyscale
* if available in the input. To guarantee a binary image use BinaryImage.
* NOTE that in order to give the best possible image, the bounds are
* expanded slightly over the binary connected component, by the supplied
* padding, so the top-left position of the returned image is returned
* in (left,top). These will most likely not match the coordinates
* returned by BoundingBox.
* Use pixDestroy to delete the image after use.
*/
Pix* PageIterator::GetImage(PageIteratorLevel level, int padding,
int* left, int* top) const {
int right, bottom;
if (!BoundingBox(level, left, top, &right, &bottom))
return NULL;
Pix* pix = tesseract_->pix_grey();
if (pix == NULL)
return GetBinaryImage(level);
// Expand the box.
*left = MAX(*left - padding, 0);
*top = MAX(*top - padding, 0);
right = MIN(right + padding, rect_width_);
bottom = MIN(bottom + padding, rect_height_);
Box* box = boxCreate(*left, *top, right - *left, bottom - *top);
Pix* grey_pix = pixClipRectangle(pix, box, NULL);
boxDestroy(&box);
if (level == RIL_BLOCK) {
Pix* mask = it_->block()->block->render_mask();
Pix* expanded_mask = pixCreate(right - *left, bottom - *top, 1);
pixRasterop(expanded_mask, padding, padding,
pixGetWidth(mask), pixGetHeight(mask),
PIX_SRC, mask, 0, 0);
pixDestroy(&mask);
pixDilateBrick(expanded_mask, expanded_mask, 2*padding + 1, 2*padding + 1);
pixInvert(expanded_mask, expanded_mask);
pixSetMasked(grey_pix, expanded_mask, 255);
pixDestroy(&expanded_mask);
}
return grey_pix;
}
示例4: main
main(int argc,
char **argv)
{
char *infile, *outfile;
l_int32 d;
PIX *pixs, *pixgr, *pixb;
static char mainName[] = "showedges";
if (argc != 3)
exit(ERROR_INT(" Syntax: showedges infile outfile", mainName, 1));
infile = argv[1];
outfile = argv[2];
pixs = pixRead(infile);
d = pixGetDepth(pixs);
if (d != 8 && d != 32)
exit(ERROR_INT("d not 8 or 32 bpp", mainName, 1));
pixgr = pixHalfEdgeByBandpass(pixs, SMOOTH_WIDTH_1, SMOOTH_WIDTH_1,
SMOOTH_WIDTH_2, SMOOTH_WIDTH_2);
pixb = pixThresholdToBinary(pixgr, THRESHOLD);
pixInvert(pixb, pixb);
/* pixWrite("junkpixgr", pixgr, IFF_JFIF_JPEG); */
pixWrite(outfile, pixb, IFF_PNG);
return 0;
}
示例5: PixTestEqual
void
PixTestEqual(PIX *pixs1,
PIX *pixs2,
PIX *pixm,
l_int32 set,
l_int32 connectivity)
{
l_int32 same;
PIX *pixc11, *pixc12, *pixc21, *pixc22, *pixmi;
pixmi = pixInvert(NULL, pixm);
pixc11 = pixCopy(NULL, pixs1);
pixc12 = pixCopy(NULL, pixs1);
pixc21 = pixCopy(NULL, pixs2);
pixc22 = pixCopy(NULL, pixs2);
/* Test inverse seed filling */
pixSeedfillGrayInv(pixc11, pixm, connectivity);
pixSeedfillGrayInvSimple(pixc12, pixm, connectivity);
pixEqual(pixc11, pixc12, &same);
if (same)
fprintf(stderr, "\nSuccess for inv set %d\n", set);
else
fprintf(stderr, "\nFailure for inv set %d\n", set);
/* Test seed filling */
pixSeedfillGray(pixc21, pixm, connectivity);
pixSeedfillGraySimple(pixc22, pixm, connectivity);
pixEqual(pixc21, pixc22, &same);
if (same)
fprintf(stderr, "Success for set %d\n", set);
else
fprintf(stderr, "Failure for set %d\n", set);
/* Display the filling results */
/* pixDisplay(pixc11, 220 * (set - 1), 100);
pixDisplay(pixc21, 220 * (set - 1), 320); */
pixDestroy(&pixmi);
pixDestroy(&pixc11);
pixDestroy(&pixc12);
pixDestroy(&pixc21);
pixDestroy(&pixc22);
return;
}
示例6: Java_com_googlecode_leptonica_android_Morphology_nativeTophat
jint Java_com_googlecode_leptonica_android_Morphology_nativeTophat(JNIEnv *env,
jclass clazz,
jint nativePix) {
LOGV("%s",__FUNCTION__);
PIX *pixs = (PIX *) nativePix;
if(pixs==0){
LOGI("pixs is 0");
}
PIX* pixsg = pixConvertRGBToLuminance(pixs);
LOGI("luminance");
if(pixsg==0){
LOGI("lumi error");
}
/* Black tophat (closing - original-image) and invert */
PIX* pixg = pixTophat(pixsg, 15, 15, L_TOPHAT_BLACK);
LOGI("tophat");
if(pixg==0){
LOGI("tophat error");
}
pixInvert(pixg, pixg);
LOGI("invert");
if(pixg==0){
LOGI("invert error");
}
/* Set black point at 200, white point at 245. */
PIX* pixd = pixGammaTRC(NULL, pixg, 1.0, 200, 245);
LOGI("gamma");
if(pixd==0){
LOGI("gamma error");
}
pixDestroy(&pixsg);
pixDestroy(&pixg);
return (jint) pixd;
}
示例7: main
main(int argc,
char **argv)
{
PIX *pixs, *pixsg, *pixg, *pixd;
PIXA *pixa;
static char mainName[] = "livre_tophat";
if (argc != 1)
return ERROR_INT(" Syntax: livre_tophat", mainName, 1);
/* Read the image in at 150 ppi. */
if ((pixs = pixRead("brothers.150.jpg")) == NULL)
return ERROR_INT("pix not made", mainName, 1);
pixDisplayWrite(NULL, -1);
pixDisplayWriteFormat(pixs, 2, IFF_JFIF_JPEG);
pixsg = pixConvertRGBToLuminance(pixs);
/* Black tophat (closing - original-image) and invert */
pixg = pixTophat(pixsg, 15, 15, L_TOPHAT_BLACK);
pixInvert(pixg, pixg);
pixDisplayWriteFormat(pixg, 2, IFF_JFIF_JPEG);
/* Set black point at 200, white point at 245. */
pixd = pixGammaTRC(NULL, pixg, 1.0, 200, 245);
pixDisplayWriteFormat(pixd, 2, IFF_JFIF_JPEG);
pixDestroy(&pixg);
pixDestroy(&pixd);
/* Generate the output image */
pixa = pixaReadFiles("/tmp", "junk_write_display");
pixd = pixaDisplayTiledAndScaled(pixa, 8, 350, 3, 0, 25, 2);
pixWrite("/tmp/tophat.jpg", pixd, IFF_JFIF_JPEG);
pixDisplay(pixd, 0, 0);
pixDestroy(&pixd);
pixDestroy(&pixs);
pixDestroy(&pixsg);
return 0;
}
示例8: PrepareDistortedPix
// Creates and returns a Pix distorted by various means according to the bool
// flags. If boxes is not nullptr, the boxes are resized/positioned according to
// any spatial distortion and also by the integer reduction factor box_scale
// so they will match what the network will output.
// Returns nullptr on error. The returned Pix must be pixDestroyed.
Pix* PrepareDistortedPix(const Pix* pix, bool perspective, bool invert,
bool white_noise, bool smooth_noise, bool blur,
int box_reduction, TRand* randomizer,
GenericVector<TBOX>* boxes) {
Pix* distorted = pixCopy(nullptr, const_cast<Pix*>(pix));
// Things to do to synthetic training data.
if (invert && randomizer->SignedRand(1.0) < 0)
pixInvert(distorted, distorted);
if ((white_noise || smooth_noise) && randomizer->SignedRand(1.0) > 0.0) {
// TODO(rays) Cook noise in a more thread-safe manner than rand().
// Attempt to make the sequences reproducible.
srand(randomizer->IntRand());
Pix* pixn = pixAddGaussianNoise(distorted, 8.0);
pixDestroy(&distorted);
if (smooth_noise) {
distorted = pixBlockconv(pixn, 1, 1);
pixDestroy(&pixn);
} else {
distorted = pixn;
}
}
if (blur && randomizer->SignedRand(1.0) > 0.0) {
Pix* blurred = pixBlockconv(distorted, 1, 1);
pixDestroy(&distorted);
distorted = blurred;
}
if (perspective)
GeneratePerspectiveDistortion(0, 0, randomizer, &distorted, boxes);
if (boxes != nullptr) {
for (int b = 0; b < boxes->size(); ++b) {
(*boxes)[b].scale(1.0f / box_reduction);
if ((*boxes)[b].width() <= 0)
(*boxes)[b].set_right((*boxes)[b].left() + 1);
}
}
return distorted;
}
示例9: pixWriteMemBmp
//.........这里部分代码省略.........
pquad->blue = pquad->green = pquad->red = val;
pquad->alpha = 255; /* opaque */
}
}
}
#if DEBUG
{l_uint8 *pcmptr;
pcmptr = (l_uint8 *)pixGetColormap(pix)->array;
fprintf(stderr, "Pix colormap[0] = %c%c%c%d\n",
pcmptr[0], pcmptr[1], pcmptr[2], pcmptr[3]);
fprintf(stderr, "Pix colormap[1] = %c%c%c%d\n",
pcmptr[4], pcmptr[5], pcmptr[6], pcmptr[7]);
}
#endif /* DEBUG */
offbytes = BMP_FHBYTES + BMP_IHBYTES + cmaplen;
fsize = offbytes + fimagebytes;
fdata = (l_uint8 *)LEPT_CALLOC(fsize, 1);
*pfdata = fdata;
*pfsize = fsize;
/* Convert to little-endian and write the file header data */
bmpfh = (BMP_FH *)fdata;
bmpfh->bfType = convertOnBigEnd16(BMP_ID);
bmpfh->bfSize = convertOnBigEnd16(fsize & 0x0000ffff);
bmpfh->bfFill1 = convertOnBigEnd16((fsize >> 16) & 0x0000ffff);
bmpfh->bfOffBits = convertOnBigEnd16(offbytes & 0x0000ffff);
bmpfh->bfFill2 = convertOnBigEnd16((offbytes >> 16) & 0x0000ffff);
/* Convert to little-endian and write the info header data */
bmpih = (BMP_IH *)(fdata + BMP_FHBYTES);
bmpih->biSize = convertOnBigEnd32(BMP_IHBYTES);
bmpih->biWidth = convertOnBigEnd32(w);
bmpih->biHeight = convertOnBigEnd32(h);
bmpih->biPlanes = convertOnBigEnd16(1);
bmpih->biBitCount = convertOnBigEnd16(fdepth);
bmpih->biSizeImage = convertOnBigEnd32(fimagebytes);
bmpih->biXPelsPerMeter = convertOnBigEnd32(xres);
bmpih->biYPelsPerMeter = convertOnBigEnd32(yres);
bmpih->biClrUsed = convertOnBigEnd32(ncolors);
bmpih->biClrImportant = convertOnBigEnd32(ncolors);
/* Copy the colormap data and free the cta if necessary */
if (ncolors > 0) {
memcpy(fdata + BMP_FHBYTES + BMP_IHBYTES, cta, cmaplen);
if (heapcm) LEPT_FREE(cta);
}
/* When you write a binary image with a colormap
* that sets BLACK to 0, you must invert the data */
if (fdepth == 1 && cmap && ((l_uint8 *)(cmap->array))[0] == 0x0) {
pixInvert(pix, pix);
}
/* An endian byte swap is also required */
pixEndianByteSwap(pix);
/* Transfer the image data. Image origin for bmp is at lower right. */
fmdata = fdata + offbytes;
if (fdepth != 24) { /* typ 1 or 8 bpp */
data = (l_uint8 *)pixGetData(pix) + pixBpl * (h - 1);
for (i = 0; i < h; i++) {
memcpy(fmdata, data, fBpl);
data -= pixBpl;
fmdata += fBpl;
}
} else { /* 32 bpp pix; 24 bpp file
* See the comments in pixReadStreamBmp() to
* understand the logic behind the pixel ordering below.
* Note that we have again done an endian swap on
* little endian machines before arriving here, so that
* the bytes are ordered on both platforms as:
Red Green Blue --
|-----------|------------|-----------|-----------|
*/
extrabytes = fBpl - 3 * w;
line = pixGetData(pix) + pixWpl * (h - 1);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
pword = line + j;
pel[2] = *((l_uint8 *)pword + COLOR_RED);
pel[1] = *((l_uint8 *)pword + COLOR_GREEN);
pel[0] = *((l_uint8 *)pword + COLOR_BLUE);
memcpy(fmdata, &pel, 3);
fmdata += 3;
}
if (extrabytes) {
for (k = 0; k < extrabytes; k++) {
memcpy(fmdata, &pel, 1);
fmdata++;
}
}
line -= pixWpl;
}
}
pixDestroy(&pix);
return 0;
}
示例10: TestDistance
static void
TestDistance(PIXA *pixa,
PIX *pixs,
l_int32 conn,
l_int32 depth,
l_int32 bc,
l_int32 *pcount,
L_REGPARAMS *rp)
{
PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5;
/* Test the distance function and display */
pixInvert(pixs, pixs);
pixt1 = pixDistanceFunction(pixs, conn, depth, bc);
regTestWritePixAndCheck(pixt1, IFF_PNG, pcount, rp);
pixSaveTiled(pixt1, pixa, 1, 1, 20, 0);
pixInvert(pixs, pixs);
pixt2 = pixMaxDynamicRange(pixt1, L_LOG_SCALE);
regTestWritePixAndCheck(pixt2, IFF_JFIF_JPEG, pcount, rp);
pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
pixDestroy(&pixt1);
pixDestroy(&pixt2);
/* Test the distance function and display with contour rendering */
pixInvert(pixs, pixs);
pixt1 = pixDistanceFunction(pixs, conn, depth, bc);
regTestWritePixAndCheck(pixt1, IFF_PNG, pcount, rp);
pixSaveTiled(pixt1, pixa, 1, 1, 20, 0);
pixInvert(pixs, pixs);
pixt2 = pixRenderContours(pixt1, 2, 4, 1); /* binary output */
regTestWritePixAndCheck(pixt2, IFF_PNG, pcount, rp);
pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
pixt3 = pixRenderContours(pixt1, 2, 4, depth);
pixt4 = pixMaxDynamicRange(pixt3, L_LINEAR_SCALE);
regTestWritePixAndCheck(pixt4, IFF_JFIF_JPEG, pcount, rp);
pixSaveTiled(pixt4, pixa, 1, 0, 20, 0);
pixt5 = pixMaxDynamicRange(pixt3, L_LOG_SCALE);
regTestWritePixAndCheck(pixt5, IFF_JFIF_JPEG, pcount, rp);
pixSaveTiled(pixt5, pixa, 1, 0, 20, 0);
pixDestroy(&pixt1);
pixDestroy(&pixt2);
pixDestroy(&pixt3);
pixDestroy(&pixt4);
pixDestroy(&pixt5);
/* Label all pixels in each c.c. with a color equal to the
* max distance of any pixel within that c.c. from the bg.
* Note that we've normalized so the dynamic range extends
* to 255. For the image here, each unit of distance is
* represented by about 21 grayscale units. The largest
* distance is 12. */
if (depth == 8) {
pixt1 = pixDistanceFunction(pixs, conn, depth, bc);
pixt4 = pixMaxDynamicRange(pixt1, L_LOG_SCALE);
regTestWritePixAndCheck(pixt4, IFF_JFIF_JPEG, pcount, rp);
pixSaveTiled(pixt4, pixa, 1, 1, 20, 0);
pixt2 = pixCreateTemplate(pixt1);
pixSetMasked(pixt2, pixs, 255);
regTestWritePixAndCheck(pixt2, IFF_JFIF_JPEG, pcount, rp);
pixSaveTiled(pixt2, pixa, 1, 0, 20, 0);
pixSeedfillGray(pixt1, pixt2, 4);
pixt3 = pixMaxDynamicRange(pixt1, L_LINEAR_SCALE);
regTestWritePixAndCheck(pixt3, IFF_JFIF_JPEG, pcount, rp);
pixSaveTiled(pixt3, pixa, 1, 0, 20, 0);
pixDestroy(&pixt1);
pixDestroy(&pixt2);
pixDestroy(&pixt3);
pixDestroy(&pixt4);
}
return;
}
示例11: pixFindColorRegionsLight
//.........这里部分代码省略.........
if (colordiff < 0) colordiff = 40;
/* Check if pixm covers most of the image. If so, just return. */
pixGetDimensions(pixs, &w, &h, NULL);
if (pixm) {
pixCountPixels(pixm, &count, NULL);
ratio = (l_float32)count / ((l_float32)(w) * h);
if (ratio > 0.7) {
if (pixadb) L_INFO("pixm has big fg: %f5.2\n", procName, ratio);
return 0;
}
}
/* Make a mask pixm1 over the dark pixels in the image:
* convert to gray using the average of the components;
* threshold using %darkthresh; do a small dilation;
* combine with pixm. */
pix1 = pixConvertRGBToGray(pixs, 0.33, 0.34, 0.33);
if (pixadb) pixaAddPix(pixadb, pixs, L_COPY);
if (pixadb) pixaAddPix(pixadb, pix1, L_COPY);
pixm1 = pixThresholdToBinary(pix1, darkthresh);
pixDilateBrick(pixm1, pixm1, 7, 7);
if (pixadb) pixaAddPix(pixadb, pixm1, L_COPY);
if (pixm) {
pixOr(pixm1, pixm1, pixm);
if (pixadb) pixaAddPix(pixadb, pixm1, L_COPY);
}
pixDestroy(&pix1);
/* Convert to gray using the minimum component value and
* find the gray value at rank 0.95, that represents the light
* pixels in the image. If it is too dark, quit. */
pix1 = pixConvertRGBToGrayMinMax(pixs, L_SELECT_MIN);
pix2 = pixInvert(NULL, pixm1); /* pixels that are not dark */
pixGetRankValueMasked(pix1, pix2, 0, 0, factor, 0.95, &val95, &nah);
pixDestroy(&pix2);
if (pixadb) {
L_INFO("val at 0.95 rank = %5.1f\n", procName, val95);
gplotSimple1(nah, GPLOT_PNG, "/tmp/lept/histo1", "gray histo");
pix3 = pixRead("/tmp/lept/histo1.png");
pix4 = pixExpandReplicate(pix3, 2);
pixaAddPix(pixadb, pix4, L_INSERT);
pixDestroy(&pix3);
}
lightbg = (l_int32)val95 >= lightthresh;
numaDestroy(&nah);
if (!lightbg) {
pixDestroy(&pix1);
pixDestroy(&pixm1);
return 0;
}
/* Make mask pixm2 over pixels that are darker than val95 - mindiff. */
pixm2 = pixThresholdToBinary(pix1, val95 - mindiff);
if (pixadb) pixaAddPix(pixadb, pixm2, L_COPY);
pixDestroy(&pix1);
/* Make a mask pixm3 over pixels that have some color saturation,
* with a (max - min) component difference >= %colordiff,
* and combine using AND with pixm2. */
pix2 = pixConvertRGBToGrayMinMax(pixs, L_CHOOSE_MAXDIFF);
pixm3 = pixThresholdToBinary(pix2, colordiff);
pixDestroy(&pix2);
pixInvert(pixm3, pixm3); /* need pixels above threshold */
if (pixadb) pixaAddPix(pixadb, pixm3, L_COPY);
pixAnd(pixm2, pixm2, pixm3);
示例12: pixReadStreamPng
//.........这里部分代码省略.........
cmap = pixcmapCreate(d); /* spp == 1 */
for (cindex = 0; cindex < num_palette; cindex++) {
rval = palette[cindex].red;
gval = palette[cindex].green;
bval = palette[cindex].blue;
pixcmapAddColor(cmap, rval, gval, bval);
}
}
else
cmap = NULL;
if ((pix = pixCreate(w, h, d)) == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return (PIX *)ERROR_PTR("pix not made", procName, NULL);
}
wpl = pixGetWpl(pix);
data = pixGetData(pix);
pixSetColormap(pix, cmap);
if (spp == 1) { /* copy straight from buffer to pix */
for (i = 0; i < h; i++) {
line = data + i * wpl;
rowptr = row_pointers[i];
for (j = 0; j < rowbytes; j++) {
SET_DATA_BYTE(line, j, rowptr[j]);
}
}
}
else { /* spp == 3 or spp == 4 */
for (i = 0; i < h; i++) {
ppixel = data + i * wpl;
rowptr = row_pointers[i];
for (j = k = 0; j < w; j++) {
SET_DATA_BYTE(ppixel, COLOR_RED, rowptr[k++]);
SET_DATA_BYTE(ppixel, COLOR_GREEN, rowptr[k++]);
SET_DATA_BYTE(ppixel, COLOR_BLUE, rowptr[k++]);
if (spp == 4)
SET_DATA_BYTE(ppixel, L_ALPHA_CHANNEL, rowptr[k++]);
ppixel++;
}
}
}
#if DEBUG
if (cmap) {
for (i = 0; i < 16; i++) {
fprintf(stderr, "[%d] = %d\n", i,
((l_uint8 *)(cmap->array))[i]);
}
}
#endif /* DEBUG */
/* If there is no colormap, PNG defines black = 0 and
* white = 1 by default for binary monochrome. Therefore,
* since we use the opposite definition, we must invert
* the image colors in either of these cases:
* (i) there is no colormap (default)
* (ii) there is a colormap which defines black to
* be 0 and white to be 1.
* We cannot use the PNG_TRANSFORM_INVERT_MONO flag
* because that flag (since version 1.0.9) inverts 8 bpp
* grayscale as well, which we don't want to do.
* (It also doesn't work if there is a colormap.)
* If there is a colormap that defines black = 1 and
* white = 0, we don't need to do anything.
*
* How do we check the polarity of the colormap?
* The colormap determines the values of black and
* white pixels in the following way:
* if black = 1 (255), white = 0
* 255, 255, 255, 0, 0, 0, 0, 0, 0
* if black = 0, white = 1 (255)
* 0, 0, 0, 0, 255, 255, 255, 0
* So we test the first byte to see if it is 0;
* if so, invert the colors.
*
* If there is a colormap, after inverting the pixels it is
* necessary to destroy the colormap. Otherwise, if someone were
* to call pixRemoveColormap(), this would cause the pixel
* values to be inverted again!
*/
if (d == 1 && (!cmap || (cmap && ((l_uint8 *)(cmap->array))[0] == 0x0))) {
/* fprintf(stderr, "Inverting binary data on png read\n"); */
pixInvert(pix, pix);
pixDestroyColormap(pix);
}
xres = png_get_x_pixels_per_meter(png_ptr, info_ptr);
yres = png_get_y_pixels_per_meter(png_ptr, info_ptr);
pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5)); /* to ppi */
pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5)); /* to ppi */
/* Get the text if there is any */
png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
if (num_text && text_ptr)
pixSetText(pix, text_ptr->text);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
return pix;
}
示例13: main
int main(int argc,
char **argv)
{
char dilateseq[512], erodeseq[512];
char openseq[512], closeseq[512];
char wtophatseq[512], btophatseq[512];
l_int32 w, h;
PIX *pixs, *pix1, *pix2, *pix3, *pix4, *pix5;
PIXA *pixa;
PIXACC *pacc;
PIXCMAP *cmap;
L_REGPARAMS *rp;
if (regTestSetup(argc, argv, &rp))
return 1;
pixs = pixRead("aneurisms8.jpg");
pixa = pixaCreate(0);
/* =========================================================== */
/* -------- Test gray morph, including interpreter ------------ */
pix1 = pixDilateGray(pixs, WSIZE, HSIZE);
sprintf(dilateseq, "D%d.%d", WSIZE, HSIZE);
pix2 = pixGrayMorphSequence(pixs, dilateseq, 0, 0);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 0 */
regTestComparePix(rp, pix1, pix2); /* 1 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
pix1 = pixErodeGray(pixs, WSIZE, HSIZE);
sprintf(erodeseq, "E%d.%d", WSIZE, HSIZE);
pix2 = pixGrayMorphSequence(pixs, erodeseq, 0, 100);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 2 */
regTestComparePix(rp, pix1, pix2); /* 3 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
pix1 = pixOpenGray(pixs, WSIZE, HSIZE);
sprintf(openseq, "O%d.%d", WSIZE, HSIZE);
pix2 = pixGrayMorphSequence(pixs, openseq, 0, 200);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 4 */
regTestComparePix(rp, pix1, pix2); /* 5 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
pix1 = pixCloseGray(pixs, WSIZE, HSIZE);
sprintf(closeseq, "C%d.%d", WSIZE, HSIZE);
pix2 = pixGrayMorphSequence(pixs, closeseq, 0, 300);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 6 */
regTestComparePix(rp, pix1, pix2); /* 7 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
pix1 = pixTophat(pixs, WSIZE, HSIZE, L_TOPHAT_WHITE);
sprintf(wtophatseq, "Tw%d.%d", WSIZE, HSIZE);
pix2 = pixGrayMorphSequence(pixs, wtophatseq, 0, 400);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 8 */
regTestComparePix(rp, pix1, pix2); /* 9 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
pix1 = pixTophat(pixs, WSIZE, HSIZE, L_TOPHAT_BLACK);
sprintf(btophatseq, "Tb%d.%d", WSIZE, HSIZE);
pix2 = pixGrayMorphSequence(pixs, btophatseq, 0, 500);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 10 */
regTestComparePix(rp, pix1, pix2); /* 11 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
/* ------------- Test erode/dilate duality -------------- */
pix1 = pixDilateGray(pixs, WSIZE, HSIZE);
pix2 = pixInvert(NULL, pixs);
pix3 = pixErodeGray(pix2, WSIZE, HSIZE);
pixInvert(pix3, pix3);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 12 */
regTestComparePix(rp, pix1, pix3); /* 13 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
pixDestroy(&pix3);
/* ------------- Test open/close duality -------------- */
pix1 = pixOpenGray(pixs, WSIZE, HSIZE);
pix2 = pixInvert(NULL, pixs);
pix3 = pixCloseGray(pix2, WSIZE, HSIZE);
pixInvert(pix3, pix3);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 14 */
regTestComparePix(rp, pix1, pix3); /* 15 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
pixDestroy(&pix3);
/* ------------- Test tophat duality -------------- */
pix1 = pixTophat(pixs, WSIZE, HSIZE, L_TOPHAT_WHITE);
pix2 = pixInvert(NULL, pixs);
pix3 = pixTophat(pix2, WSIZE, HSIZE, L_TOPHAT_BLACK);
regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 16 */
regTestComparePix(rp, pix1, pix3); /* 17 */
pixaAddPix(pixa, pix1, L_INSERT);
pixDestroy(&pix2);
//.........这里部分代码省略.........
示例14: pixGenerateSelRandom
//.........这里部分代码省略.........
{
l_int32 ws, hs, w, h, x, y, i, j, thresh;
l_uint32 val;
PIX *pixt1, *pixt2, *pixfg, *pixbg;
SEL *seld, *sel;
PROCNAME("pixGenerateSelRandom");
if (ppixe) *ppixe = NULL;
if (!pixs)
return (SEL *)ERROR_PTR("pixs not defined", procName, NULL);
if (pixGetDepth(pixs) != 1)
return (SEL *)ERROR_PTR("pixs not 1 bpp", procName, NULL);
if (hitfract <= 0.0 && missfract <= 0.0)
return (SEL *)ERROR_PTR("no hits or misses", procName, NULL);
if (hitfract > 1.0 || missfract > 1.0)
return (SEL *)ERROR_PTR("fraction can't be > 1.0", procName, NULL);
if (distance <= 0)
distance = DEFAULT_DISTANCE_TO_BOUNDARY;
if (distance > MAX_DISTANCE_TO_BOUNDARY) {
L_WARNING("distance too large; setting to max value", procName);
distance = MAX_DISTANCE_TO_BOUNDARY;
}
/* Locate the foreground */
pixClipToForeground(pixs, &pixt1, NULL);
if (!pixt1)
return (SEL *)ERROR_PTR("pixt1 not made", procName, NULL);
ws = pixGetWidth(pixt1);
hs = pixGetHeight(pixt1);
w = ws;
h = hs;
/* Crop out a region including the foreground, and add pixels
* on sides depending on the side flags */
if (toppix || botpix || leftpix || rightpix) {
x = y = 0;
if (toppix) {
h += toppix;
y = toppix;
}
if (botpix)
h += botpix;
if (leftpix) {
w += leftpix;
x = leftpix;
}
if (rightpix)
w += rightpix;
pixt2 = pixCreate(w, h, 1);
pixRasterop(pixt2, x, y, ws, hs, PIX_SRC, pixt1, 0, 0);
}
else
pixt2 = pixClone(pixt1);
if (ppixe)
*ppixe = pixClone(pixt2);
pixDestroy(&pixt1);
/* Identify fg and bg pixels that are at least 'distance' pixels
* away from the boundary pixels in their set */
seld = selCreateBrick(2 * distance + 1, 2 * distance + 1,
distance, distance, SEL_HIT);
pixfg = pixErode(NULL, pixt2, seld);
pixbg = pixDilate(NULL, pixt2, seld);
pixInvert(pixbg, pixbg);
selDestroy(&seld);
pixDestroy(&pixt2);
/* Generate the sel from a random selection of these points */
sel = selCreateBrick(h, w, h / 2, w / 2, SEL_DONT_CARE);
if (hitfract > 0.0) {
thresh = (l_int32)(hitfract * (l_float64)RAND_MAX);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
pixGetPixel(pixfg, j, i, &val);
if (val) {
if (rand() < thresh)
selSetElement(sel, i, j, SEL_HIT);
}
}
}
}
if (missfract > 0.0) {
thresh = (l_int32)(missfract * (l_float64)RAND_MAX);
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
pixGetPixel(pixbg, j, i, &val);
if (val) {
if (rand() < thresh)
selSetElement(sel, i, j, SEL_MISS);
}
}
}
}
pixDestroy(&pixfg);
pixDestroy(&pixbg);
return sel;
}
示例15: DoPageSegmentation
l_int32
DoPageSegmentation(PIX *pixs, /* should be at least 300 ppi */
l_int32 which) /* 1, 2, 3, 4 */
{
char buf[256];
l_int32 zero;
BOXA *boxatm, *boxahm;
PIX *pixr; /* image reduced to 150 ppi */
PIX *pixhs; /* image of halftone seed, 150 ppi */
PIX *pixm; /* image of mask of components, 150 ppi */
PIX *pixhm1; /* image of halftone mask, 150 ppi */
PIX *pixhm2; /* image of halftone mask, 300 ppi */
PIX *pixht; /* image of halftone components, 150 ppi */
PIX *pixnht; /* image without halftone components, 150 ppi */
PIX *pixi; /* inverted image, 150 ppi */
PIX *pixvws; /* image of vertical whitespace, 150 ppi */
PIX *pixm1; /* image of closed textlines, 150 ppi */
PIX *pixm2; /* image of refined text line mask, 150 ppi */
PIX *pixm3; /* image of refined text line mask, 300 ppi */
PIX *pixb1; /* image of text block mask, 150 ppi */
PIX *pixb2; /* image of text block mask, 300 ppi */
PIX *pixnon; /* image of non-text or halftone, 150 ppi */
PIX *pix1, *pix2, *pix3, *pix4;
PIXA *pixa;
PIXCMAP *cmap;
PTAA *ptaa;
l_int32 ht_flag = 0;
l_int32 ws_flag = 0;
l_int32 text_flag = 0;
l_int32 block_flag = 0;
PROCNAME("DoPageSegmentation");
if (which == 1)
ht_flag = 1;
else if (which == 2)
ws_flag = 1;
else if (which == 3)
text_flag = 1;
else if (which == 4)
block_flag = 1;
else
return ERROR_INT("invalid parameter: not in [1...4]", procName, 1);
pixa = pixaCreate(0);
lept_mkdir("lept/livre");
/* Reduce to 150 ppi */
pix1 = pixScaleToGray2(pixs);
if (ws_flag || ht_flag || block_flag) pixaAddPix(pixa, pix1, L_COPY);
if (which == 1)
pixWrite("/tmp/lept/livre/orig.gray.150.png", pix1, IFF_PNG);
pixDestroy(&pix1);
pixr = pixReduceRankBinaryCascade(pixs, 1, 0, 0, 0);
/* Get seed for halftone parts */
pix1 = pixReduceRankBinaryCascade(pixr, 4, 4, 3, 0);
pix2 = pixOpenBrick(NULL, pix1, 5, 5);
pixhs = pixExpandBinaryPower2(pix2, 8);
if (ht_flag) pixaAddPix(pixa, pixhs, L_COPY);
if (which == 1)
pixWrite("/tmp/lept/livre/htseed.150.png", pixhs, IFF_PNG);
pixDestroy(&pix1);
pixDestroy(&pix2);
/* Get mask for connected regions */
pixm = pixCloseSafeBrick(NULL, pixr, 4, 4);
if (ht_flag) pixaAddPix(pixa, pixm, L_COPY);
if (which == 1)
pixWrite("/tmp/lept/livre/ccmask.150.png", pixm, IFF_PNG);
/* Fill seed into mask to get halftone mask */
pixhm1 = pixSeedfillBinary(NULL, pixhs, pixm, 4);
if (ht_flag) pixaAddPix(pixa, pixhm1, L_COPY);
if (which == 1) pixWrite("/tmp/lept/livre/htmask.150.png", pixhm1, IFF_PNG);
pixhm2 = pixExpandBinaryPower2(pixhm1, 2);
/* Extract halftone stuff */
pixht = pixAnd(NULL, pixhm1, pixr);
if (which == 1) pixWrite("/tmp/lept/livre/ht.150.png", pixht, IFF_PNG);
/* Extract non-halftone stuff */
pixnht = pixXor(NULL, pixht, pixr);
if (text_flag) pixaAddPix(pixa, pixnht, L_COPY);
if (which == 1) pixWrite("/tmp/lept/livre/text.150.png", pixnht, IFF_PNG);
pixZero(pixht, &zero);
if (zero)
fprintf(stderr, "No halftone parts found\n");
else
fprintf(stderr, "Halftone parts found\n");
/* Get bit-inverted image */
pixi = pixInvert(NULL, pixnht);
if (ws_flag) pixaAddPix(pixa, pixi, L_COPY);
if (which == 1) pixWrite("/tmp/lept/livre/invert.150.png", pixi, IFF_PNG);
/* The whitespace mask will break textlines where there
* is a large amount of white space below or above.
* We can prevent this by identifying regions of the
* inverted image that have large horizontal (bigger than
//.........这里部分代码省略.........