Showing posts with label Image Processing. Show all posts
Showing posts with label Image Processing. Show all posts

1/28/2015

Canny edge detector, example source code in opencv

Canny edge processing example



cpu version result



gpu version result






...
cpu version code.
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
//#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

void ProccTimePrint( unsigned long Atime , string msg);

int main()  
{  

 //video input
 VideoCapture cap("C:\\videoSample\\tracking\\rouen_video.avi");

 //variable
 Mat o_frame;  
 Mat showMat_r;  
 Mat showMat_r2;  
 

 //first frame
 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   


 unsigned long AAtime=0;
 namedWindow("origin",0);
 namedWindow("canny",0);

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  AAtime = getTickCount();  

  //frame
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  

  resize(o_frame, showMat_r, Size(RWIDTH, RHEIGHT) );  
  Canny(showMat_r, showMat_r2, 50, 100);

  imshow("origin", showMat_r);  
  imshow("canny", showMat_r2);  

  //processing time
  ProccTimePrint(AAtime , "Total");     

  if( waitKey(5) > 0)  
   break;  

 }

 return 0;
}


void ProccTimePrint( unsigned long Atime , string msg)     
{     
 unsigned long Btime=0;     
 float sec, fps;     
 Btime = getTickCount();     
 sec = (Btime - Atime)/getTickFrequency();     
 fps = 1/sec;     
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );     
}

///

gpu version code
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

void ProccTimePrint( unsigned long Atime , string msg);

int main()  
{  

 //input
 VideoCapture cap("C:\\videoSample\\tracking\\rouen_video.avi");

 //variable
 Mat o_frame;  
 Mat showMat_r;  
 Mat showMat_r2;  
 gpu::GpuMat o_frame_gpu;
 gpu::GpuMat r_frame_gpu;
 gpu::GpuMat rg_frame_gpu;
 gpu::GpuMat r_frame_gpu2;

 //first frame
 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   


 unsigned long AAtime=0;

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  AAtime = getTickCount();  

  //frame
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  

  //upload to gpumat
  o_frame_gpu.upload(o_frame);  
  gpu::resize(o_frame_gpu, r_frame_gpu, Size(RWIDTH, RHEIGHT) );  
  gpu::cvtColor(r_frame_gpu, rg_frame_gpu, CV_BGR2GRAY);
  gpu::Canny(rg_frame_gpu, r_frame_gpu2, 50, 100); //gray only

  //download to mat
  r_frame_gpu.download(showMat_r);  
  r_frame_gpu2.download(showMat_r2);  

  
  //show image
  imshow("origin", showMat_r);  
  imshow("canny", showMat_r2);  

  //processing time
  ProccTimePrint(AAtime , "Total");     

  if( waitKey(10) > 0)  
   break;  

 }

 return 0;
}


void ProccTimePrint( unsigned long Atime , string msg)     
{     
 unsigned long Btime=0;     
 float sec, fps;     
 Btime = getTickCount();     
 sec = (Btime - Atime)/getTickFrequency();     
 fps = 1/sec;     
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );     
}



1/02/2013

Image Normalization using Standard deviation - example source code

You can understand Standard deviation normalization, referenced on this web page.
http://www.d.umn.edu/~deoka001/Normalization.html
This web page introduce that..(wrote by Siddharth Deokar)

-------------------------------------------------------------------------
Normalization by Standard Deviation
We normalize the attribute values by using standard deviation.

For Example:

Consider 5 instances which has attribute A with the follwing values: {-5, 6, 9, 2, 4}

First we calculate the mean as follows:

Mean = (-5+6+9+2+4) / 5 = 3.2

Second, we subtract the mean from all the values and square them:

(-5-3.2)^2 = 67.24
(6-3.2)^2 = 7.84
(9-3.2)^2 = 33.64
(2-3.2)^2 = 1.44
(4-3.2)^2 = 0.64

Then we find the deviation as follows:

Deviation = sqrt ((67.24 + 7.84 + 33.64 + 1.44 + 0.64) / 5) = 4.71

Now we normalize the attribute values:

x => (x - Mean) / Deviation

-5 => (-5 - 3.2) / 4.71 = -1.74
6 => (6 - 3.2) / 4.71 = 0.59
9 => (9 - 3.2) / 4.71 = 1.23
2 => (2 - 3.2) / 4.71 = -0.25
4 => (4 - 3.2) / 4.71 = 0.17

-------------------------------------------------------------------------



This is sample source code:

//

int i,j,z;
 double sum=0; 
 double Mean;
 
 //sum, mean
 for(j=0; j< Height; ++j)
 {
  for(i=0; i< Width; ++i)
  {
   sum = sum + Pdata[i][j];
  }
 }

 Mean = sum/(Height*Width);
 
 //Deviation
 double dSum=0;
 double deviation;
 double std_dev; 
 
 for(j=0; j< Height; ++j)
 {
  for(i=0; i< Width; ++i)
  {        
   Pdata[i][j] = (Pdata[i][j] - Mean);

   dSum = dSum + (Pdata[i][j]*Pdata[i][j]);
  }
 }
 
 deviation = dSum / (Width*Height);
 std_dev = sqrt(deviation);
 
 //Normalization
 for(j=0; j< Height; ++j)
 {
  for(i=0; i< Width; ++i)
  {        
   Pdata[i][j] = (Pdata[i][j] / std_dev);
  }
 }

11/06/2012

YUV color format (444, 422, 411) - simple explanation

In the YUV color format, Y is bright information, U is blue color area, V is red color area.
Show the below picture. The picture is u-v color map

 
 
There are 3 type color format of YUV.
The types are 444, 422, 411.
 
 
444 is one pixel is 24 bits. 
 
 
 
 
422 is one pixel is 16 bits.
And U0, V0 is shared by Y0, Y1.



411 is one pixel is 12 bits.
 
 
 
 
This source code is convert code from YUV422 to RGB.
 

/ 


void ConvertYUV422toRGB(unsigned char** pSrc, unsigned char** pDst,int width,int height){
int i,j;
unsigned char* dst=*pDst;
unsigned char* src=*pSrc;
int Y1,Y2,U,V;
int R,G,B;

for(i=0;i2)
{
Y1=src[i*2+1];
Y2=src[i*2+3];
U=src[i*2];
V=src[i*2+2];

dst[i*3+2]=(unsigned char)((float)Y1+(1.4075f*(float)(V-128)));
dst[i*3+1]=(unsigned char)((float)Y1+(0.3455f*(float)(U-128)-(0.7169f*(float)(V-128))));
dst[i*3]=(unsigned char)((float)Y1+(1.7790f*(float)(U-128)));
dst[i*3+5]=(unsigned char)((float)Y2+(1.4075f*(float)(V-128)));
dst[i*3+4]=(unsigned char)((float)Y2+(0.3455f*(float)(U-128)-(0.7169f*(float)(V-128))));
dst[i*3+3]=(unsigned char)((float)Y2+(1.7790f*(float)(U-128)));
}
}

 


And YUV is converted by below fomulation.

R = Y + 1.4075(V-128)
G = Y - 0.3455(U-128) - 0.7169(V-128)
B = Y + 1.7790(U-128)
 
 
 
Thanks you.
 


sorry last source code is broken.
becasue html tag..
So I upload new version convert source code.
note!!! check Y position, the position is different as machine environment..
In the source code, stride means real width size.
YUV422 to RGB
--
//yuv to rgb
 int Y,U,V;   
 int R1,G1,B1;
 for(int ih=0; ihm_height; ++ih)
 {   
  //int iww=0;
  for(int iw=0; iwm_width; iw++)
  {
  
   if(iw %2 == 0)
   {
    Y = obj->m_yuv[ih*obj->m_stride + iw*2 + 0];
    U = obj->m_yuv[ih*obj->m_stride + iw*2 + 1];
    V = obj->m_yuv[ih*obj->m_stride + iw*2 + 3];
   }else{
    Y = obj->m_yuv[ih*obj->m_stride + iw*2 + 0];
    U = obj->m_yuv[ih*obj->m_stride + iw*2 -1];
    V = obj->m_yuv[ih*obj->m_stride + iw*2 + 1];
   }
   
   R1 = Y + 1.4075*(V-128);
   G1 = Y - 0.3455*(U-128) - 0.7169*(V-128);
   B1 = Y + 1.7790*(U-128);

   o_Image2.at(ih, iw)[0] = BYTE(MMIN(255, MMAX(0, B1)));// B1;
   o_Image2.at(ih, iw)[1] = BYTE(MMIN(255, MMAX(0, G1)));// G1;
   o_Image2.at(ih, iw)[2] = BYTE(MMIN(255, MMAX(0, R1)));// R1;
  }
 }
--
RGB to YUV422
--
int Y,U,V;   
   int R1,G1,B1;
   for(int ih=0; ih< obj->m_height; ++ih)
   {   
    //int iww=0;
    for(int iw=0; iw< obj->m_width; iw++)
    {

     B1 = o_Image2.at< cv::Vec3b>(ih, iw)[0];// B1;
     G1 = o_Image2.at< cv::Vec3b>(ih, iw)[1];// G1;
     R1 = o_Image2.at< cv::Vec3b>(ih, iw)[2];// R1;

     Y = (0.257*R1) + (0.504*G1) + (0.098*B1) +16;
     U = -(0.148*R1) - (0.291*G1) + (0.439*B1) + 128;
     V = (0.439*R1 ) - (0.368*G1) - (0.071*B1) + 128;
     Y = MMIN(255, MMAX(0, Y));
     U = MMIN(255, MMAX(0, U));
     V = MMIN(255, MMAX(0, V));

     if(iw %2 == 0)
     {
      obj->m_yuv[ih*obj->m_stride + iw*2 + 0] = Y;
      obj->m_yuv[ih*obj->m_stride + iw*2 + 1] = U;
      obj->m_yuv[ih*obj->m_stride + iw*2 + 3] = V;
     }else{
      obj->m_yuv[ih*obj->m_stride + iw*2 + 0] = Y;
      obj->m_yuv[ih*obj->m_stride + iw*2 -1] = U;
      obj->m_yuv[ih*obj->m_stride + iw*2 + 1] = V;
     }     
    }
--
 


9/02/2011

Simple Image Processing Tool / C++ Source(MFC, OpenCV) / κ°„λ‹¨ν•œ μ˜μƒμ²˜λ¦¬ 툴

Created Date : 2008.10
Language : C++(MFC)
Tool : Visual C++ 6.0
Library & Utilized : OpenCV 1.0
Reference :  Image Processing Book, Learning OpenCV
etc. : -



When I teach Image Processing to undergraduate students,  this program is made for practicing programming.
This code includes simple image processing method.

ex) Image adding, reverse, Lookup Table making, Gamma adjusting, Image Zoom in/out, Rotation, Morphology, Histogram Drawing, Stretching, Laplacian, sovel, RGB, HSI, YCbCr..

The code may be useful to beginner studying image processing , MFC and OpenCV.


<source code>



If you have good idea or advanced opinion, please reply me. Thank you

(Please understand my bad english ability. If you point out my mistake, I would correct pleasurably. Thank you!!)---------------------------------------------------------------------------



κ°„λ‹¨ν•œ μ˜μƒ 처리 μ†ŒμŠ€
RAW λ₯Ό μ˜€ν”ˆν•˜μ—¬
2μ˜μƒ λ”ν•˜κΈ°, 역상계산, 룩업데이블 생성, 감마 쑰절
μ˜μƒ μΆ•μ†Œ, μ˜μƒν™•λŒ€, μ˜μƒ νšŒμ „
λͺ¨ν΄λ‘œμ§€, 히슀트그램 그리기, 슀트레칭, ν‰ν™œν™”
λΌν”ŒλΌμ‹œμ•ˆ, 평화화 μ†Œλ²¨
을 μ²˜λ¦¬ν•¨
λ˜ν•œ BMP νŒŒμΌμ„ μ˜€ν”ˆν•˜μ—¬ RGB, HSI, Ycbcr둜 채널을 λΆ„λ¦¬ν•œλ‹€.
그리고 OpenCVλ₯Ό μ΄μš©ν•˜μ—¬
μ˜μƒ 밝기 쑰절, 크기 쑰절, νšŒμ „, 이진화, 팽창, μˆ˜μΆ•, ν•„ν„°λ₯Ό μ μš©ν•΄ λ³Έλ‹€.

μ••μΆ• νŒŒμΌμ— μ†ŒμŠ€, 이미지, κ°„λž΅ν•œ μ„€λͺ… λ¬Έμ„œκ°€ 있음



<source code>



쒋은 μ˜κ²¬μ΄λ‚˜ λ‹΅λ³€ 남겨 μ£Όμ„Έμš”.

8/22/2011

Mini PhotoShop / Image Processing Tool / C++ source (MFC) / λ―Έλ‹ˆ 포토샡, 이미지 ν”„λ‘œμ„Έμ‹± 툴


Created Date : 2003.3
Language : C/C++
Tool : Microsoft Visual C++ 6.0 (MFC)
Library & Utilized : -
Reference :  Simplified approach to image prcessing book -Randy Crane-
etc. : Bmp Image Files







Many ages ago, when I studied image processing firstly, I made this program for training programming skill and studying  image processing.

This program include variety image processing functions.
For example

Filter function - blur,highpass, median, embossing, edge gaussian noise, morphology, the window that support preview function and the value of mask.
Geometry function - flip, rotation, zoom in/out
Channel split - RGB, HSI, CMYK, YIQ, YUW, YUV, YCbCr, the window that support preview function.
Color adjustment - Contrast, Brightness, HSI, Gamma, Red-Green tint, Reference, colorfulness
Histogram - binary, logarithm, auto contrast strech, end in contrast stretch.
etc - undo,redo function, skin color detection, labeling

You can download entire source code -> <Here>

Plz reply your opinion.
Thank you.

(Please understand my bad english ability. If you point out my mistake, I would correct pleasurably. Thank you!!)


-----------------------------------------------------


처음 μ˜μƒμ²˜λ¦¬ 곡뢀λ₯Ό μ‹œμž‘ν–ˆμ„ λ•Œ ν”„λ‘œκ·Έλž˜λ°κ³Ό 이미지 처리 방법을 κ³΅λΆ€ν•˜κΈ° μœ„ν•΄ λ§Œλ“  ν”„λ‘œκ·Έλž¨ μž…λ‹ˆλ‹€.

이 ν”„λ‘œκ·Έλž¨μ€  bmp νŒŒμΌν¬λ§·μ„ 자유둭게 λ‹€λ£°μˆ˜ μžˆλŠ” 클래슀λ₯Ό μ œμž‘ν•˜μ—¬ λ‹€μŒκ³Ό 같은 μ—¬λŸ¬κ°€μ§€ 이미지 처리 κΈ°λŠ₯을 κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.
ν•„ν„°κΈ°λŠ₯ - blur,highpass, median, embossing, edge gaussian noise, morphology.
필터결과와 λ§ˆμŠ€ν¬κ°’μ„ 미리보기 κΈ°λŠ₯μ°½.
μ΄λ―Έμ§€μ‘°μž‘ - ν”Œλ¦½, λ°˜μ „, νšŒμ „, ν™•λŒ€, μΆ•μ†Œ
체널 뢄리 - RGB, HSI, CMYK, YIQ, YUW, YUV, YCbCr, λΆ„λ¦¬λœ 채널을 ν•œλˆˆμœΌλ‘œ λ³Όμˆ˜μžˆλŠ” κΈ°λŠ₯μ°½.
μƒ‰μ‘°μ ˆ - Contrast, Brightness, HSI, Gamma, Red-Green tint, Reference, colorfulness.
νžˆμŠ€ν† κ·Έλž¨ - 이진화, logarithm, auto contrast strech,end in contrast stretch.
기타 - undo,redoκΈ°λŠ₯ μ‚΄μƒ‰μΆ”μΆœ, 라벨링.

이 ν”„λ‘œκ·Έλž¨μ€ μ—¬κΈ°μ—μ„œ 받을 수 μžˆμŠ΅λ‹ˆλ‹€. <Here>

κ°œμ„  μ‚¬ν•­μ΄λ‚˜ 쒋은 의견 μžˆμœΌμ‹œλ©΄ λ‹΅λ³€ μ£Όμ„Έμš”.
κ°μ‚¬ν•©λ‹ˆλ‹€.


8/21/2011

Split color channels -> RGB, HSI, YCbCr, YUV, YIQ, YUW / C++ source / 칼라 채널 뢄리

Created Date : 2007.8
Language : C/C++
Tool : Microsoft Visual C++ 6.0 (MFC)
Library & Utilized : -
Reference : Simplified approach to image processing book -Randy Crane-
etc. : Bmp Image File
                                            RGB                                                   YCbCr


 
                                            YIQ                                                      YUV

                                             HSI                                                       YUW    

This program splits the channel into several version.
There are many color channels for image processing such as RGB, HSI, YCbCr, YUV, YIQ, YUW.
Each channels is made by specific formula that is introduced from many image processing books. The method of covert is simple and not difficult.

For example YCbCr is made by blow formula.
Y = 0.299*R + 0.587*G + 0.114*B
Cb = -0.16874*R + -0.33126*G + 0.5*B
Cr = 0.5*R + -0.41869*G - 0.08131*B

You can download entire source code and report document(doc) but the report is written by korean (sorry). -> <here>

If you have any question or opinion for improving, please reply.
Thank you.

(Please understand my bad english ability. If you point out my mistake, I would correct pleasurably. Thank you!!)
-------------------------------------

이 ν”„λ‘œκ·Έλž¨μ€ bmpνŒŒμΌμ„ μ—΄μ–΄μ„œ μ—¬λŸ¬κ°€μ§€ μ±„λ„λ‘œ λΆ„λ¦¬ν•˜λŠ” κ°„λ‹¨ν•œ μ˜μƒ 처리 ν”„λ‘œκ·Έλž¨μž…λ‹ˆλ‹€.
색 채널 λΆ„λ¦¬λŠ” 이미지 처리 μ±…μ—μ„œ μ‰½κ²Œ κ·Έ 방법과 원리λ₯Ό μ°Ύμ•„ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
예λ₯Ό λ“€μ–΄ RGBμ—μ„œ YCbCr둜 λΆ„λ¦¬ν•˜λŠ” 방법은 μ•„λž˜ μˆ˜μ‹μœΌλ‘œ κ°€λŠ₯ν•©λ‹ˆλ‹€.

Y = 0.299*R + 0.587*G + 0.114*B
Cb = -0.16874*R + -0.33126*G + 0.5*B
Cr = 0.5*R + -0.41869*G - 0.08131*B


더 μžμ„Έν•œ λ‚΄μš©μ€ λ§ν¬ν•œ μ €μ˜ 전체 μ†ŒμŠ€ μ½”λ“œλ₯Ό μ²¨λΆ€ν•˜μ„Έμš”.
μ••μΆ• νŒŒμΌμ—λŠ” ν•œκΈ€λ‘œ 된 λ³΄κ³ μ„œλ„ ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. - > <here>

였래 전에 λ§Œλ“  μ†ŒμŠ€λΌ 뢀쑱함이 λ§Žμ„ 것 κ°™λ„€μš”.
λ¬Έμ œμ μ΄λ‚˜ κ°œμ„  사항 있으면 λ‹΅λ³€ μ£Όμ„Έμš”.
κ°μ‚¬ν•©λ‹ˆλ‹€.