The previous days of MATLAB, used to deal with digital images, matrix operations, if you forget the linear algebra is really GG.
After using MATLAB to be deeply spit groove, decided to switch to opencv,c++ seems to be a good tool for processing digital images
1. Install OpenCV on Ubuntu
The tutorial can be Baidu's, are very detailed
2. Pixel processing Image:
(1) According to the input data, using the neighboring sampling interpolation method, scaling the image
Principle of proximity sampling interpolation method: http://blog.chinaunix.net/uid-27675161-id-3452025.html
(2) Change the grayscale resolution of the digital image according to the input
(3) The operation of the experiment with gray-scale image to achieve
3. Direct code + Comment
1 //My_hw1.cpp2#include <stdio.h>3#include <iostream>4#include <opencv2/opencv.hpp>5#include <opencv2/core/core.hpp>6#include <opencv2/highgui/highgui.hpp>7 8 using namespaceCV;9 using namespacestd;Ten One stringMy_pic ="07.png"; AMat image =Imread (My_pic, cv_load_image_grayscale); - - voidOn_trackbar (intPvoid*) { the - //************************scale*************************** - //Reload the image every time the callback is loaded -Mat src =Imread (My_pic, cv_load_image_grayscale); + - //gets the value of the three slider bars + intc = Cvgettrackbarpos ("Width"," after change"); A intR = Cvgettrackbarpos ("Height"," after change"); at intLevel = Cvgettrackbarpos (" Level"," after change"); - - //record the width height of the original graph, and the corresponding point Sx,sy (used in the neighboring sampling interpolation method) - intSH =src.rows; - intSW =Src.cols; - intsx, SY; in - //new picture after reset to Mat DST (R, C, Src.type ()); + - //fill the pixels of the new picture the for(inti =0; I < R; i + +) { * for(intj =0; J < C; J + +) { $ Doublex = I/(R +0.0);Panax Notoginseng Doubley = J/(C +0.0); -SX = sh *x; theSY = SW *y; +Dst.at<uchar> (i, j) = Src.at<uchar>(sx,sy); A } the } + - //********************quantization************************ $ $ //make the value of level legal - if(Level <= the&& level >= -) level = -; - Else if(Level < -&& level >= -) level = -; the Else if(Level < -&& level >= +) level = +; - Else if(Level < +&& level >= -) level = -;Wuyi Else if(Level < -&& level >=8) level =8; the Else if(Level <8&& level >=4) level =4; - ElseLevel =2; Wu - intChannels =dst.channels (); About intnrows =dst.rows; $ intNcols = Dst.cols *channels; - -Uchar table[ the]; - intdegree =255/(Level-1); A intNumber = the/Level ; + intCount =0; the intValue =0; - $ //set the required grayscale mapping the for(inti =0; I < the; i + +, Count + +) { the if(Count < number) Table[i] =value; the Else { theCount =0; -Value + =degree; inTable[i] =value; the } the } About the if(Src.iscontinuous ()) { theNcols *=nrows; thenrows =1; + } - the for(inti =0; i < nrows; i + +) {BayiUchar *p = dst.ptr<uchar>(i); the for(intj =0; J < Ncols; J + +) { theP[J] =Table[p[j]]; - } - } the the //cout << level << "<< C <<" "<< r <<" \ n "; theImshow (" after change", DST); the } - the intMainintargcChar**argv) { theImshow ("before change", image); the 94 intwidth =384; the intHeight = the; the intLevel = the; theNamedwindow (" after change", cv_window_autosize);98Createtrackbar ("Width"," after change", &width, -, On_trackbar); AboutCreatetrackbar ("Height"," after change", &height, -, On_trackbar); -Createtrackbar (" Level"," after change", &level, the, On_trackbar);//Slider101 102On_trackbar (0,0);103 104 Waitkey (); the return 0;106 }107
In addition, I choose the compilation method is CMake + make;
CMakeLists.txt
1 2.8 )2Project (MY_HW1)3find_package (OpenCV REQUIRED)4add_ Executable (my_hw1 my_hw1.cpp)5 target_link_libraries (MY_HW1 ${opencv_libs})
CMakeLists.txt
Put the corresponding picture in the folder of the same directory, go to the folder, enter the command line
CMake.
Make
./my_hw1
You can execute it ~ ~
Finally, put up a run result diagram:
[OpenCV] pixel manipulation of digital image processing