Sharing simple examples of C ++ Image Processing code and image processing examples

Source: Internet
Author: User

Sharing simple examples of C ++ Image Processing code and image processing examples

Decoder: stb_image

Https://github.com/nothings/stb/blob/master/stb_image.h

Encoder: tiny_jpeg
Https://github.com/serge-rgb/TinyJPEG/blob/master/tiny_jpeg.h

Stb_image.h is used to parse the image format:
JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC

Tiny_0000.h is used to save the JPG format.

This example demonstrates a simple inverse color processing algorithm and briefly comments some logics.

Complete code:

// If it is Windows, call the system API ShellExecuteA to open the image # if defined (_ MSC_VER) # define _ CRT_SECURE_NO_WARNINGS # include <windows. h> # define USE_SHELL_OPEN # endif # define STB_IMAGE_STATIC # define STB_IMAGE_IMPLEMENTATION # include "stb_image.h" // ref: https://github.com/nothings/stb/blob/master/stb_image.h#define TJE_IMPLEMENTATION # include "tiny_assist.h" // ref: https://github.com/serge-rgb/TinyJPEG/blob/master/tiny_jpeg.h#inclu De <math. h> # include <io. h> # include <iostream> # include <string> # include <chrono> // timing auto const epoch = std: chrono: steady_clock: now (); static double now () {return std: chrono: duration_cast <std: chrono: milliseconds> (std: chrono: steady_clock: now ()-epoch ). count ()/1000.0 ;}; template <typename FN> static double evaluate (const FN & fn) {auto took =-now (); return (fn (), took + now ();} // stores the changes in the current file location Std: string m_curFilePath; // load image void loadImage (const char * filename, unsigned char * & Output, int & Width, int & Height, int & Channels) {Output = stbi_load (filename, & Width, & Height, & Channels, 0) ;}// Save the image void saveImage (const char * filename, int Width, int Height, int Channels, unsigned char * Output, bool open = true) {std: string saveFile = m_curFilePath; saveFile + = filename; // save as jpg if (! Tje_encode_to_file (saveFile. c_str (), Width, Height, Channels, Output) {fprintf (stderr, "failed to write the JPEG file. \ n "); return ;}# ifdef USE_SHELL_OPEN if (open) ShellExecuteA (NULL," open ", saveFile. c_str (), NULL, NULL, SW_SHOW); # else // not implemented on other platforms for the moment # endif} // obtain the currently passed File Location void getCurrentFilePath (const char * filePath, std:: string & curFilePath) {char drive [_ MAX_DRIVE]; char dir [_ MAX_DIR]; char fname [_ MAX_FNAME]; char ext [_ MAX_EXT]; curFilePath. clear (); _ splitpath_s (filePath, drive, dir, fname, ext); curFilePath + = drive; curFilePath + = dir; curFilePath + = fname; curFilePath + = "_";} // algorithm processing. Here we use a reversed color as an example void processImage (unsigned char * Input, unsigned char * Output, unsigned int Width, unsigned int Height, unsigned int Channels) {int Stride = Width * Channels; if (Channels = 1) {for (unsigned int Y = 0; Y <Height; Y ++) {unsigned char * scanLineOut = Output + (Y * Stride); unsigned char * scanLineIn = Input + (Y * Stride); for (unsigned int X = 0; X <Width; X ++) {scanLineOut [0] = 255-scanLineIn [0]; scanLineIn ++; scanLineOut ++ ;}}} else if (Channels = 3 | Channels = 4) {for (unsigned int Y = 0; Y <Height; Y ++) {unsigned char * scanLineOut = Output + (Y * Stride); unsigned char * scanLineIn = Input + (Y * Stride); for (unsigned int X = 0; X <Width; X ++) {scanLineOut [0] = 255-scanLineIn [0]; scanLineOut [1] = 255-scanLineIn [1]; scanLineOut [2] = 255-scanLineIn [2]; // when the number of channels is 4, do not process A-channel reversed colors (scanLineOut [3] = 255-scanLineIn [3]; scanLineIn + = Channels; scanLineOut + = Channels ;}}} int main (int argc, char ** argv) {std: cout <"Image Processing" <std: endl; std: cout <"blog: http://cpuimage.cnblog S.com/"<std: endl; std: cout <" resolution supported by slice format: "<std: endl; std: cout <" JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC "<std: endl; // check whether the parameters are correct if (argc <2) {std :: cout <"parameter error. "<Std: endl; std: cout <" drag and drop the file to the executable file, or use the command line: imageProc.exe image "<std: endl; std: cout <"Example: imageProc.exe d: \ image.jpg" <std: endl; return 0;} std: string szfile = argv [1]; // check whether the input file contains if (_ access (szfile. c_str (), 0) =-1) {std: cout <"the input file does not exist. The parameter is incorrect! "<Std: endl;} getCurrentFilePath (szfile. c_str (), m_curFilePath); int Width = 0; // Image Width int Height = 0; // Image Height int Channels = 0; // Number of image channels unsigned char * inputImage = NULL; // input image pointer double nLoadTime = bytes ([&] {// load image loadImage (szfile. c_str (), inputImage, Width, Height, Channels) ;}); std: cout <"loading time:" <int (nLoadTime * 1000) <"millisecond" <std: endl; if (Channels! = 0) & (Width! = 0) & (Height! = 0) {// allocate and load the same memory for processing and output the result unsigned char * outputImg = (unsigned char *) stbi _ malloc (Width * Channels * Height * sizeof (unsigned char); if (inputImage) {// if the image is loaded successfully, the content is copied to the output memory, memcpy (outputImg, inputImage, Width * Channels * Height);} else {std: cout <"Load file: \ n" <szfile. c_str () <"failed! "<Std: endl;} double nProcessTime = equals ([&] {// processing algorithm processImage (inputImage, outputImg, Width, Height, Channels);}); std:: cout <"processing time:" <int (nProcessTime * 1000) <"millisecond" <std: endl; // Save the processed image double nSaveTime = watermark ([&] {saveImage ("_done.jpg", Width, Height, Channels, outputImg);}); std :: cout <"Storage Duration:" <int (nSaveTime * 1000) <"millisecond" <std: endl; // release the occupied memory if (outputImg) {Stbi_image_free (outputImg); outputImg = NULL;} if (inputImage) {stbi_image_free (inputImage); inputImage = NULL ;}} else {std: cout <"Load file: \ n "<szfile. c_str () <"failed! "<Std: endl;} getchar (); std: cout <" press any key to exit the program \ n "<std: endl; return EXIT_SUCCESS ;}

The specific process is as follows:

Load images (drag and drop files to executable files)-> algorithm processing-> Save images-> open and save images (Windows only)

The three steps of loading, processing, and saving are time-consuming computing and output.

 

Reference resources: http://www.cnblogs.com/tntmonks/p/5305844.html

 

Later, bloggers will share a series of articles on cpu-based image algorithms based on this template.

Related Article

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.