"Exercise 5.6" diffuse fill method, Thresholding, Cvsaveimage save format

Source: Internet
Author: User

Outline
Topic Requirements
Program code
Result picture

Title Requirements:

Creates a clear mask from the noise image. After exercise 5 is complete, keep the largest graphics area in the drawing. Set a pointer in the upper-left corner of the image and let him traverse the image. When you find that the pixel value is 255, store its position and then fill it with a new color value of 100. read out the contiguous area returned by the diffuse fill method and record its area. If there is another large area in the image, fill the relatively small area with a value of 0, and then delete the recorded area. If the new area is larger than the previous area, fill the previous area with a value of 0 and delete his position. Finally, the remaining maximum area is filled with a color value of 255, showing the result. Now get a mask, which is the only contiguous region.

Program code:

1 //OpenCVExerciseTesting.cpp: Defines the entry point of the console application. 2 //3 //d:\\work\\work_programming\\source\\image\\lena.jpg4 5 6#include"stdafx.h"7#include <cv.h>8#include 9#include <iostream>Ten using namespaceCV; One using namespacestd; A  - structRegionInfo - { the     intx; -     inty; -     DoubleArea ; - }; +  - //function declaration---->-->--->-->--->-->--->// +  A DoubleFloodfillimage (Iplimage * img,intXinty, Cvscalar newval); at voidDiscardthesmallregion (Iplimage * img, regionInfo regionnow, RegionInfo * largerregion, RegionInfo *smallregion); -  - //<--<--<--<--<--<--<--<--<--Function Declaration// -  -  - int_tmain (intARGC, _tchar*argv[]) in { - RegionInfo largerregion, Smallregion,regionnow; toLargerregion.area =dbl_min; +Smallregion.area =Dbl_max; -  the     Const Char* FileName1 ="d:\\work\\work_programming\\source\\image\\opencvexerciseimage\\ 5th Chapter \\ExerciseResult_5-5.jpg"; *Iplimage * Src1 =cvloadimage (fileName1, cv_load_image_grayscale); $ assert (SRC1);Panax Notoginseng  -       theCvnamedwindow ("Original Image",0);  +Cvnamedwindow ("Topic _a",0); A  theCvshowimage ("Original Image", SRC1); +       -     //---------------------------A: Start--------------------------------// $  $Iplimage * Imgcopy =cvcloneimage (SRC1); -     //Cvzero (DIFF12); -     //If you do not use the Cvthreshold function, the resulting result is not expected because some of the pixels in the picture are greater than 0 but small y in 255 the     //originally this previous question saved the picture is the threshold, but after the preservation distortion, uses the Cvsaveimage PNG format non-compression mode to avoid this problem -Cvthreshold (Src1, Imgcopy, -,255, cv_thresh_binary);Wuyi  the     intx =-1; -     inty =-1; Wu     DoubleAreanow; -      for(inth =0; H < imgcopy->height;++h) About     { $uchar* ptr = (uchar*) (imgcopy->imagedata + H * imgcopy->widthstep); -          for(intW =0; W < imgcopy->width; ++W) -         { -             if(Imgcopy->nchannels = =1) A             { +                 if(Ptr[w] = =255) the                 { -                     //at first, W was assigned to Y, and H was assigned to X, resulting in a result that was not what you wanted, and this kind of problem is hard to find . $x =W; they =h; theAreanow = Floodfillimage (imgcopy, x, Y, Cvscalar ( -)); the  theregionnow.x =x; -REGIONNOW.Y =y; inRegionnow.area =Areanow; the  theDiscardthesmallregion (Imgcopy,regionnow, &largerregion, &smallregion); About                 } the             } the         } the     } +  -Cvfloodfill (Imgcopy, Cvpoint (largerregion.x, LARGERREGION.Y), Cvscalar (255), Cvscalarall (0.0), Cvscalarall (0.0), NULL,8); the BayiCvshowimage ("Topic _a", imgcopy); the  the  -     //---------------------------A: End--------------------------------//     -  theCvwaitkey (0); the  theCvreleaseimage (&src1); theCvreleaseimage (&imgcopy); -  theCvdestroywindow ("Original Image");  theCvdestroywindow ("Topic _a"); the 94     return 0; the } the   the DoubleFloodfillimage (Iplimage * img,intXinty,cvscalar newval)98 { About Cvconnectedcomp Connectinfo; -     intFlags =8;101Cvfloodfill (IMG, cvpoint (x, y), newval, Cvscalarall (2.0), Cvscalarall (0.0), &connectinfo, flags);102     returnConnectinfo.area;103 }104  the voidDiscardthesmallregion (Iplimage * img, regionInfo regionnow, RegionInfo * largerregion, RegionInfo *smallregion)106 {107     if(Regionnow.area >= largerregion->Area )108     {109Smallregion->x = largerregion->x; theSmallregion->y = largerregion->y;111Smallregion->area = largerregion->Area ; the 113Largerregion->x =regionnow.x; theLargerregion->y =regionnow.y; theLargerregion->area =Regionnow.area;  the     }117     Else118     {119Smallregion->x =regionnow.x; -Smallregion->y =regionnow.y;121Smallregion->area =Regionnow.area;122     }123 124     if(Smallregion->area! =dbl_min) the     {126         //floodfillimage (IMG, smallregion->x, smallregion->y, cvscalar (0));127Cvfloodfill (IMG, Cvpoint (smallregion->x, Smallregion->y), Cvscalar (0), Cvscalarall (0.0), Cvscalarall (0.0), NULL,8); -     }129}

Result Picture:

Use the JPG format when saving the picture for the previous question, and cvsaveimage

The third parameter uses the default value, which causes the saved picture to be distorted,

Pixel values in addition to 0 and 255 there are other, so, get the right side of the

Error picture, water filled before re-threshold value in China or on the title Save with

The non-compression method can avoid this problem, the code to save the picture is shown later in this article

1     Char *    "e:\\testing\\image\\savepath\\image_close. PNG"; 2     int  params 0 ; 3     Cvsaveimage (Savename, Dirtydiff, &params);

"Exercise 5.6" diffuse fill method, Thresholding, Cvsaveimage save format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.