Image processing based on Visual C + + programming Learning notes (3) displaying grayscale images and two-valued images

Source: Internet
Author: User

Grayscale Image Imggray

With the base above, the grayscale image is fairly simple.

Color graph to Grayscale method:

1. Floating-point algorithm: gray=r*0.3+g*0.59+b*0.11
2. Integer method: gray= (r*30+g*59+b*11)/100
3. Shift Method: Gray = (r*28+g*151+b*77) >>8; ??
4. Average method: gray= (R+G+B)/3;
5. Take green only: gray=g;

Floating point algorithm is used here

voidImggray::showgray (CDC *PDC, CRect RC)    {Readbitdata (); unsignedinti,j;  for(i=0; I<vimageheight (); i++){         for(j=0; J<vimagewidth (); j + +) {unsignedCharR = img[3* (I*vimagewidth () +j)]; unsignedCharG = img[3* (I*vimagewidth () + j) +1]; unsignedCharB = img[3* (I*vimagewidth () + j) +2]; unsignedChargray= (unsignedChar)(0.30*r+0.59*g+0.11*B); R=g=b=GRAY; PDC->setpixel (RC. Width ()-vimagewidth ())/2+j, (RC. Height ()-vimageheight ())/2+I,rgb (r,g,b)); }    }}voidImggray::showgray2 (CDC *PDC, CRect RC)//Quick Display{unsignedint_img_w = Vimagewidth () *3; if(_img_w%4!=0) _img_w= (_img_w/4+1)*4; unsignedintI,j,x,y; Y=vimageheight (); X=vimagewidth ();  for(i=0; i<y;i++){         for(j=0; j<x;j++) {unsignedCharb=raw_img[i*_img_w+j*3]; unsignedCharg=raw_img[i*_img_w+j*3+1]; unsignedCharr=raw_img[i*_img_w+j*3+2]; unsignedChargray= (unsignedChar)(0.30*r+0.59*g+0.11*c); Raw_img[i*_img_w+j*3]   =GRAY; Raw_img[i*_img_w+j*3+1] =GRAY; Raw_img[i*_img_w+j*3+2] =GRAY; }} imshow2 (PDC, RC);}

Grayscale Equalization:

According to the distribution of gray scale, the gray-scale pixels are distributed evenly between 0-255 (establish gray level table), and then assign the pixels of each point according to the table.

voidImggray::grayequalize (unsignedChar*_img) {unsignedCharmap[ the]; inti,cnt[ the],scnt[ the];  for(i=0;i< the; i++) {Cnt[i]=0; Scnt[i]=0; }     for(i=0; i<data_size;i++) {Cnt[_img[i]]++; } scnt[0]=cnt[0];  for(i=1;i< the; i++) {Scnt[i]=scnt[i-1]+Cnt[i]; }     for(i=0;i< the; i++) {Map[i]=scnt[i]*255*1.0/data_size; }     for(i=0; i<data_size;i++) {_img[i]=Map[_img[i]]; }}

Imgbinary of binary image

Two value based on histogram equalization (mean method)

voidImgbinary::binary ()//Histogram based{    inti,sum=0;  for(i=0; i<data_size;i++) sum+=Img_g[i]; intavg=sum/data_size;  for(i=0; i<data_size;i++)    {        if(IMG_G[I]&GT;AVG) img_b[i]=true; Elseimg_b[i]=false; }}

Image processing based on Visual C + + programming Learning notes (3) displaying grayscale images and two-valued images

Related Article

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.