关闭

图像变换--灰度切割、位图切割

1185人阅读 评论(0) 收藏 举报
分类:

灰度切割:分为两种情况,一种情况是将某段阈值的图像设为一个较高值,其它灰度指定一个较低值。另一种情况是所需范围的灰度变亮,但仍保持图像的背景和灰度色调。

for (int i = 0; i < img_height - 1; i++)
  {
   for (int j = 0; j < img_width - 1; j++)
   {
    uchar cur = data[i * img_width + j];

    if (cur > 20 && cur < 200)
    {
     cur  = cur + 50;
     if (cur < 0)
     {
      cur =0;
     }
     else if(cur > 255)
     {
      cur =255;
     }
     data[i * img_width + j] = cur;
    }
   }
  }

位图切割:就8比物图像的位平面抽取而主,说明用一个灰度阈值变换函数处理输入图像可以获得位平面7的二值图像并不困难。该灰度阈值变换函数:(1)把图像中0和127间的所有灰度映射到一个灰度级例如(0)(2)把129到255间的灰度映射为另一种灰度级。例如(255)

#include "math.h"
#include"cv.h"
#include"highgui.h"


int main(int argc,char **argv)

 IplImage *pImg;
 IplImage *pImg_gray;
 IplImage *pImg_cut[8];
 

 if(argc == 3 && (pImg = cvLoadImage(argv[1],-1))!= 0)
 { 
  
  pImg_gray = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);


  cvCopy(pImg,pImg_gray,NULL);
  
  int img_width = pImg->width;//image width
  int img_height = pImg->height;//image height

  uchar *data;
  data = (uchar *)pImg_gray->imageData;
  uchar *data_cut[8];
  pImg_cut[0] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[1] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[2]= cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[3] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[4] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[5] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[6] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[7] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U,1);

  
  uchar zhi = 1;
  for (int k = 0; k< 8 ;k++)
  {
   data_cut[k] = (uchar *)pImg_gray->imageData;
   if (k > 0)
    zhi = zhi<<1;
   for (int i = 0; i< img_height; i++)
   {
    for (int j = 0; j< img_width ; j++)
    {
      uchar cur = data[i * img_width + j];
         cur = cur & 0x40; //按位与运算
      if (cur == 1)
      {
       cur = 255;
      }
      else cur = 0;
      data[i * img_width + j] = cur;
     
    }

   }
   cvCopy(pImg_gray,pImg_cut[k],NULL);
   cvCopy(pImg,pImg_gray,NULL);
   

  }

  cvNamedWindow("log1",1);//create window
  cvNamedWindow("log2",2);//create window
  cvNamedWindow("log3",3);//create window
  cvNamedWindow("log4",4);//create window
  cvNamedWindow("log5",5);//create window
  cvNamedWindow("log6",6);//create window
  cvNamedWindow("log7",7);//create window
  cvNamedWindow("log8",8);//create window

  cvShowImage("log1", pImg_cut[0]);
  cvShowImage("log2", pImg_cut[1]);
  cvShowImage("log3", pImg_cut[2]);
  cvShowImage("log4", pImg_cut[3]);
  cvShowImage("log5", pImg_cut[4]);
  cvShowImage("log6", pImg_cut[5]);
  cvShowImage("log7", pImg_cut[6]);
  cvShowImage("log8", pImg_cut[7]);

  cvWaitKey(0);

  cvDestroyWindow("canny");

  cvReleaseImage(&pImg);
  cvReleaseImage(&pImg_gray);
  
  return 0;
 }
 
 return -1;
}

 

 


 

 


#include "math.h"
#include"cv.h"
#include"highgui.h"
//#include <iostream>
//using namespace std;


int main(int argc,char **argv)
{
 /*IplImage *img=cvLoadImage("F:\HU\testopencv\pp.jpg");
 cvNamedWindow("Example1",CV_WINDOW_AUTOSIZE);
 cvShowImage("Example1",img);
 cvWaitKey(0);
 cvReleaseImage(&img);
 cvDestroyWindow("Example1"); */
 //图像反转
 IplImage *pImg;
 IplImage *pImg_gray;
 IplImage *pImg_cut[8];
 /*IplImage *pImg_cut0;
 IplImage *pImg_cut1;
 IplImage *pImg_cut2;
 IplImage *pImg_cut3;
 IplImage *pImg_cut4;
 IplImage *pImg_cut5;
 IplImage *pImg_cut6;
 IplImage *pImg_cut7;*/
 /*pImg_cut0 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 pImg_cut1 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 pImg_cut2 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 pImg_cut3 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 pImg_cut4 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 pImg_cut5 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 pImg_cut6 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 pImg_cut7 = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);*/
 //IplImage *pImg_bi;
 

 if(argc == 3 && (pImg = cvLoadImage(argv[1],-1))!= 0)
 { 
  
  pImg_gray = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
 // cvCvtColor(pImg,pImg_gray,CV_BGR2GRAY);
  cvCopy(pImg,pImg_gray,NULL);
  //pImg_bi = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  //cvThreshold(pImg_gray,pImg_bi,127,255  ,CV_THRESH_BINARY);
  //int a = pImg_bi->nChannels;
  //int bi_width = pImg_bi->
  int img_width = pImg->width;//image width
  int img_height = pImg->height;//image height

  uchar *data;
  data = (uchar *)pImg_gray->imageData;
  uchar *data_cut[8];
  pImg_cut[0] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[1] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[2]= cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[3] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[4] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[5] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[6] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);
  pImg_cut[7] = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1);

 /*  //for (int i = 0; i < img_height - 1; i++)
  //{
  // for (int j = 0; j < img_width - 1; j++)
  // {
  //  uchar cur = data[i * img_width + j];
  //   
  //  //cur =  3 * log((double)cur + 1.0);
  //  cur =  6 * sqrt((float)cur);
  //  if (cur < 0)
  //  {
  //   cur =0;
  //  }
  //  else if(cur > 255)
  //  {
  //   cur =255;
  //  }
  // }
  //}
  //line trans func线性变换
  float ratio = -3;//斜率
  float intercept = -838;//截距

  for (int i = 0; i < img_height - 1; i++)
  {
   for (int j = 0; j < img_width - 1; j++)
   {
    uchar cur = data[i * img_width + j];
    /*cur = ratio * cur + intercept;//线性变换

    data[i * img_width + j] = cur;
    if (cur < 0)
    {
     cur =0;
    }
    else if(cur > 255)
    {
     cur =255;
    }*/
   /* if (cur > 20 && cur < 200)
    {
     cur  = cur + 50;
     if (cur < 0)
     {
      cur =0;
     }
     else if(cur > 255)
     {
      cur =255;
     }
     data[i * img_width + j] = cur;
    }
   }
  }*/

  /*for (int i = 0; i< img_height -1 ; i++)
  {
  for (int j = 0; j< img_width -1; j++)
  {
  printf("%d",data[i * img_width + j]);
  }
  printf("\n");
  }*/
//  cvSaveImage(argv[2],pImg_bi);
  uchar zhi = 1;
  for (int k = 0; k< 8 ;k++)
  {
   data_cut[k] = (uchar *)pImg_gray->imageData;
   if (k > 0)
    zhi = zhi<<1;
   for (int i = 0; i< img_height; i++)
   {
    for (int j = 0; j< img_width ; j++)
    {
      uchar cur = data[i * img_width + j];
         cur = cur & 0x40; //按位与运算
      if (cur == 1)
      {
       cur = 255;
      }
      else cur = 0;
      data[i * img_width + j] = cur;
     
    }

   }
   cvCopy(pImg_gray,pImg_cut[k],NULL);
   cvCopy(pImg,pImg_gray,NULL);
   

  }

  cvNamedWindow("log1",1);//create window
  cvNamedWindow("log2",2);//create window
  cvNamedWindow("log3",3);//create window
  cvNamedWindow("log4",4);//create window
  cvNamedWindow("log5",5);//create window
  cvNamedWindow("log6",6);//create window
  cvNamedWindow("log7",7);//create window
  cvNamedWindow("log8",8);//create window

  cvShowImage("log1", pImg_cut[0]);
  cvShowImage("log2", pImg_cut[1]);
  cvShowImage("log3", pImg_cut[2]);
  cvShowImage("log4", pImg_cut[3]);
  cvShowImage("log5", pImg_cut[4]);
  cvShowImage("log6", pImg_cut[5]);
  cvShowImage("log7", pImg_cut[6]);
  cvShowImage("log8", pImg_cut[7]);

  cvWaitKey(0);

  cvDestroyWindow("canny");

  cvReleaseImage(&pImg);
  cvReleaseImage(&pImg_gray);
  //cvReleaseImage(&pImg_bi);
  return 0;
 }
 //printf("%s\n %s\n%s\n", argv[0],argv[1],argv[2]);
 return -1;
}

 

 


 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:410628次
    • 积分:4998
    • 等级:
    • 排名:第5866名
    • 原创:116篇
    • 转载:70篇
    • 译文:12篇
    • 评论:45条
    最新评论