OPENCV Study Notes (iv) projection

Source: Internet
Author: User

OPENCV Study Notes (iv) projection

  Optionally a picture for testing, the picture is as follows:

1#include <cv.h>2#include 3 using namespacestd;4 using namespaceCV;5 intMain ()6 {7Iplimage * src = cvloadimage ("Cat.png",0);//forced conversion read image as grayscale graph8Cvshowimage (the grayscaleImage", SRC);9Cvthreshold (SRC, SRC,0,255, Cv_thresh_otsu + cv_thresh_binary);//greater than 0 take 255, otherwise take 0Teniplimage* Paintx = cvcreateimage (cvgetsize (SRC), ipl_depth_8u,1);//image for vertical projection (single channel) Oneiplimage* Painty = cvcreateimage (cvgetsize (SRC), ipl_depth_8u,1);//image for horizontal projection (single channel) ACvzero (PAINTX);//Qing 0 -Cvzero (Painty);//Qing 0 -     int* v =New int[src->width];//used to record the number of pixel values greater than 0 in each column the     int* H =New int[src->height];//used to record the number of pixel values greater than 0 per row -memset (V,0, Src->width *4);//initializes the newly requested memory, initialized to 0,int of 4 bytes, so the block size is src->width * 4 -memset (H,0, Src->height *4);//initializes the newly requested memory, initialized to 0,int of 4 bytes, so the block size is src->height * 4 -  +     intx, y;//For Loop use -Cvscalar s, t;// +     /* A Cvscalar is an array that can be used to hold 4 double values at typically used to store pixel values (not necessarily grayscale values), can hold up to 4 channels of - How to assign a value: - a) storing pixels in a single-channel image: Cvscalar (255); - b) store three-channel image in pixels: cvscalar (255,255,255); - c) Use only the first channel, val[0]=val0, equivalent to Cvscalar (val0,0,0,0); -     */ in     //Traverse to count the number of pixels with a value greater than 0 per column -      for(x =0; x<src->width; X + +) to     { +          for(y =0; y<src->height; y++) -         { thes = cvget2d (src, y, x);//gets the pixel value of the specified coordinate *             if(s.val[0]>0) $v[x]++;//When the pixel value is greater than 0 o'clock, the pixel at the top of the column is more than 0 plus 1Panax Notoginseng         } -     } the  +     //create a vertically projected image A      for(x =0; x<src->width; X + +) the     { +          for(y =0; y<v[x]; y++) -         { $t.val[0] =255; $CVSET2D (Paintx, y, X, T);//assign a value of 255 to a coordinate with a pixel value greater than 0 -         } -     } the     //Traverse, count the number of pixels with a value greater than 0 per row -      for(y =0; y<src->height; y++)Wuyi     { the          for(x =0; x<src->width; X + +) -         { Wus = cvget2d (src, y, x);//gets the pixel value of the specified coordinate -             if(s.val[0]>0) Abouth[y]++;//when the pixel value is greater than 0 o'clock, the pixel of the current row is greater than 0 plus 1 . $         } -     } -     //Create a horizontal projected image -      for(y =0; y<src->height; y++) A     { +          for(x =0; x) the         { -t.val[0] =255; $ cvset2d (Painty, y, X, t); the         } the     } the  theCvnamedwindow ("Two value image",1); -Cvnamedwindow ("Vertical Integral projection",1); inCvnamedwindow ("Horizontal Integral projection",1); theCvshowimage ("Two value image", SRC); theCvshowimage ("Vertical Integral projection", paintx); AboutCvshowimage ("Horizontal Integral projection", painty); theCvwaitkey (0); the cvdestroyallwindows (); theCvreleaseimage (&src); +Cvreleaseimage (&paintx); -Cvreleaseimage (&painty); the     return 0;Bayi}

  Operation Result:

  1. Grayscale image

  

  2, two-value image

  

  3. Vertical Integral projection

  

  4. Horizontal Integral projection

  

OPENCV Study Notes (iv) projection

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.