[Opencv] pixel-operated digital image processing and opencv Digital Image Processing

Source: Internet
Author: User

[Opencv] pixel-operated digital image processing and opencv Digital Image Processing

A few days ago, the matlab was used to process digital images and matrix operations. If you forget linear algebra, it would actually be GG.

After using matlab, I decided to use opencv. C ++ seems to be a good tool for processing digital images.

1. Install opencv on ubuntu

The tutorials are detailed on Baidu.

2. pixel processing image:

(1) based on the input data, use the adjacent sampling interpolation method to scale the image

Principle of interpolation method of adjacent sampling: http://blog.chinaunix.net/uid-27675161-id-3452025.html

(2) Change the gray-scale resolution of Digital Images Based on Input

(3) This experiment is implemented using grayscale images.

3. paste the code and comments directly.

1 // my_hw1.cpp 2 # include <stdio. h> 3 # include <iostream> 4 # include <opencv2/opencv. hpp> 5 # include <opencv2/core. hpp> 6 # include <opencv2/highgui. hpp> 7 8 using namespace cv; 9 using namespace std; 10 11 string my_pic = "07.png"; 12 Mat image = imread (my_pic, CV_LOAD_IMAGE_GRAYSCALE); 13 14 void on_trackbar (int p, void *) {15 16 ********** * **************** 17 // The image is reloaded every time the callback is performed. 18 Mat src = imread (my_pic, CV_LOAD_IMAGE_GRAYSCALE); 19 20 // get the value of three slide bars 21 int c = cvGetTrackbarPos ("Width", "after change"); 22 int r = cvGetTrackbarPos ("Height ", "after change"); 23 int level = cvGetTrackbarPos ("Level", "after change"); 24 25 // record the width and height of the original graph, and the corresponding vertex sx, sy (used in the neighboring sampling interpolation method) 26 int sh = src. rows; 27 int sw = src. cols; 28 int sx, sy; 29 30 // reset the new image 31 Mat dst (r, c, src. type (); 32 33 // fill in the new image's pixel 34 for (int I = 0; I <r; I ++) {35 for (int j = 0; j <c; j ++) {36 double x = I/(r + 0.0); 37 double y = j/(c + 0.0); 38 sx = sh * x; 39 sy = sw * y; 40 dst. at <uchar> (I, j) = src. at <uchar> (sx, sy ); 41} 42} 43 44 // ********************** Quantization *********** * ************ 45 46 // make the level value legal 47 if (level <= 256 & level> = 128) level = 128; 48 else if (level <128 & level> = 64) level = 64; 49 else if (level <64 & level> = 32) level = 32; 50 else if (level <32 & level> = 16) level = 16; 51 else if (level <16 & level> = 8) level = 8; 52 else if (level <8 & level> = 4) level = 4; 53 else level = 2; 54 55 int channels = dst. channels (); 56 int nrows = dst. rows; 57 int ncols = dst. cols * channels; 58 59 uchar table [256]; 60 int degree = 255/(level-1); 61 int number = 256/level; 62 int count = 0; 63 int value = 0; 64 65 // set the required grayscale ing 66 for (int I = 0; I <256; I ++, count ++) {67 if (count <number) table [I] = value; 68 else {69 count = 0; 70 value + = degree; 71 table [I] = value; 72} 73} 74 75 if (src. isContinuous () {76 ncols * = nrows; 77 nrows = 1; 78} 79 80 for (int I = 0; I <nrows; I ++) {81 uchar * p = dst. ptr <uchar> (I); 82 for (int j = 0; j <ncols; j ++) {83 p [j] = table [p [j]; 84} 85} 86 87 // cout <level <"" <c <"<r <" \ n "; 88 imshow ("after change", dst); 89} 90 91 int main (int argc, char ** argv) {92 imshow ("before change", image ); 93 94 int width = 384; 95 int height = 256; 96 int level = 256; 97 namedWindow ("after change", CV_WINDOW_AUTOSIZE); 98 createTrackbar ("Width ", "after change", & width, 800, on_trackbar); 99 createTrackbar ("Height", "after change", & height, 500, on_trackbar); 100 createTrackbar ("Level ", "after change", & level, 256, on_trackbar); // slider101 102 on_trackbar (0, 0); 103 104 waitKey (); 105 return 0; 106} 107

In addition, the compilation method I selected is cmake + make;

CMakeLists.txt

1 release (VERSION 2.8) 2 project (my_hw1) 3 find_package (OpenCV REQUIRED) 4 add_executable (my_hw1 my_hw1.cpp) 5 target_link_libraries (my_hw1 $ {OpenCV_LIBS })CMakeLists.txt

Place the corresponding image in the same directory, enter the folder, and enter the command line

Cmake.

Make

./My_hw1

You can execute it ~~

 

Finally, the following figure shows the running result:

 

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.