My opencv Study Notes (3): simple image processing with Operation pixels: Adding pretzels, image flip, changing contrast, and image sharpening

Source: Internet
Author: User
Tags image flip scalar

In the second lecture, I introduced how to operate each pixel. This operation pixel is used to complete simple image processing operations.

First, we start by adding salt and pepper noise to the image. Salt and pepper noise is actually to make some random pixels of the image black (255) or white (0 ):

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>using namespace cv;void salt(Mat& image, int n){    for(int k=0; k<n; k++){        int i = rand()%image.cols;        int j = rand()%image.rows;                if(image.channels() == 1){            image.at<uchar>(j,i) = 255;        }else{            image.at<Vec3b>(j,i)[0] = 255;            image.at<Vec3b>(j,i)[1] = 255;            image.at<Vec3b>(j,i)[2] = 255;        }    }}

In the main program, the main program uses medium filtering to eliminate the noise impact:

# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <opencv2/imgproc. HPP> using namespace CV; void salt (MAT &, int n = 3000); int main () {mat image = imread ("D:/picture/IMG. TIF "); salt (image, 500); CV: namedwindow (" image "); CV: imshow (" image ", image ); // test the filtering method to eliminate the pretzels mat result; MAT kernel; int ddepth; int kernel_size; ddepth =-1; int ind = 0; while (true) {// refresh the image once every 0.5 seconds int c = waitkey (500); // Press ESC to exit the program if (char) C = 27) {break ;} kernel_size = 3 + 2 * (IND % 5); kernel = mat: ones (kernel_size, kernel_size, cv_32f)/(float) (kernel_size * kernel_size); filter2d (image, result, ddepth, kernel); imshow ("filter result", result); ind ++;} waitkey (0 );}

It can be seen that the larger the core of the filter, the better the noise elimination effect, but the blurrier the image contour.

 

Image flip:

# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <iostream> using namespace CV; using namespace STD; int main () {// declare a variable to hold the image. The size is 0 * 0mat image; cout <"Size:" <image. size (). height <"," <image. size (). width <STD: Endl; // read an image = imread ("D:/picture/IMG. TIF "); If (! Image. Data) {STD: cout <"read image fail! "<STD: Endl;} // create an image window named" my image "namedwindow (" My image "); // display an image imshow ("My image", image); // wait for the key: the return value is the ASC code value of the key or-1 (when the wait time is reached, if no key is pressed,) waitkey (0); MAT result; result. create (image. rows, image. cols, image. type (); int rows = image. rows; int Cols = image. cols; // perform horizontal flip on the image for (INT I = 0; I <rows; I ++) {for (Int J = 0; j <Cols; j ++) {result. at <vec3b> (I, j) [0] = image. at <vec3b> (I, cols-j-1) [0]; result. at <vec3b> (I, j) [1] = image. at <vec3b> (I, cols-j-1) [1]; result. at <vec3b> (I, j) [2] = image. at <vec3b> (I, cols-j-1) [2] ;}// flip (image, result, 1 ); // positive value horizontal transformation // 0 vertical transformation // both negative values have namedwindow ("output image"); imshow ("output image", result); waitkey (0 ); return 0 ;}

The horizontal flip effect of the for loop is the same as that of the flip function.

Next, we will change the contrast and brightness of the image:

# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <iostream> using namespace CV; using namespace STD; // controls the contrast double alpha; // controls the brightness int Beta; int main () {mat image = imread ("D:/picture/IMG. TIF "); If (! Image. data) {cout <"fail to read a image" <Endl; Return-1;} mat new_image = mat: zeros (image. size (), image. type (); cout <"Basic linear transforms" <Endl; cout <"-----------------------" <Endl; cout <"* enter the Alpha value [1.0-3.0]:"; CIN> alpha; cout <"* enter the beta value [0-100]:"; cin> beta; For (INT I = 0; I <image. rows; I ++) {for (Int J = 0; j <image. cols; j ++) {for (int c = 0; C <3; C ++) {// the result may not be an integer, therefore, you need to convert the format into new_image.at <vec3b> (I, j) [C] = saturate_cast <uchar> (alpha * (image. at <vec3b> (I, j) [C]) + BETA) ;}} namedwindow ("source image"); imshow ("source image", image ); namedwindow ("result image for changing contrast and brightness"); imshow ("result image for changing contrast and brightness", new_image); waitkey (0); Return 0 ;}

The last is image sharpening:

# Include <opencv2 \ core. HPP> # include <opencv2 \ highgui. HPP> # include <opencv2/imgproc. HPP> using namespace CV; int main () {mat image; image = imread ("D:/picture/IMG. TIF ", 0); // read the gray value of the image, so the number of channels mat result is not considered in the sharpen function; result. create (image. rows, image. cols, image. type (); // use a 3*3 filter. Therefore, the traversal pixel cannot contain a circle (INT I = 1; I <image. rows-1; I ++) {// pointer of the first row, current row, and next row uchar * Previous = image. PTR <uchar> (I-1); uchar * Current = image. PTR <uchar> (I); uchar * Next = image. PTR <uchar> (I + 1); // row pointer of the output image uchar * output = result. PTR <uchar> (I); For (Int J = 1; j <image. cols-1; j ++) {// image sharpening operation * output ++ = CV :: saturate_cast <uchar> (5 * Current [J]-current [J-1]-current [J + 1]-previous [J]-next [J]); // saturate_cast <uchar> sets the value of 0 to zero, and changes the value of 255 to 255} result. row (0 ). setto (CV: Scalar (0); result. row (result. rows-1 ). setto (CV: Scalar (0); result. COL (0 ). setto (CV: Scalar (0); result. COL (result. cols-1 ). setto (CV: Scalar (0);/* // call the filter function to sharpen the image // The kernel mat kernel of the filter (3,3, cv_32f, scalar (0); // assign pixels to set kernel. at <float> (5.0) =; kernel. at <float> (1.0) =-; kernel. at <float> (1.0) =-; kernel. at <float> (1.0) =-; kernel. at <float> (1.0) =-; // call the filter function filter2d (image, result, image. depth (), kernel); */imshow ("source image", image); imshow ("Sharpen result", result); waitkey (0); Return 0 ;}

I personally think that the sharpening process for Grayscale Images is a little long, and it is more troublesome for color images. It is easier to directly call the filter function to sharpen images.

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.