Tutorial Image Processing operations using Opencv and Visual Studio

in #utopian-io7 years ago (edited)

opencv-cpp.png

<h4>What Will I Learn? <p dir="auto">This is a tutorial about Image processing functions and in this first tutorial you will learn: <ul> <li>Configure and run an opencv project in visual studio <li>Read and show image <li>Subtract two images <li>Resize <li>Accessing pixel values <li>Change pixel values <li>Create Region of interest or ROI <h4>Requirements <ul> <li>Opencv <li>Visual Studio IDE <h4>Difficulty <ul> <li>Intermediate <h4>Tutorial Contents <h2>Configuration <p dir="auto">In this tutorial I will show you some of the main functions of Opencv using VisualStudio 2015 IDE and C++ programming language.<br /> So after you Installed Visual Studio and opencv and added related environment variables, just open visual studio and create new project then select Visual C++ and Win32 Console application like the picture below:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519384897/usxh7s13gak0duwrfoaz.png" alt="tut1.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519384897/usxh7s13gak0duwrfoaz.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519384897/usxh7s13gak0duwrfoaz.png 2x" /><br /> I named the project as opencv_tutorial1, then click next and choose empty project and finish. from solution platform select x64.<br /> Then right-click on Source Files and add new item:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519385471/cusvqk3ie919tmxgkl1q.png" alt="tut3.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519385471/cusvqk3ie919tmxgkl1q.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519385471/cusvqk3ie919tmxgkl1q.png 2x" /><br /> Select C++ and create a cpp file:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443253/evp91gecehbojzmtekfo.png" alt="tut4.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443253/evp91gecehbojzmtekfo.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443253/evp91gecehbojzmtekfo.png 2x" /> <p dir="auto">Next you must select required libraries in order to use opencv libraries in your example project, so just right click on the project folder in solution explorer and select properties:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519385238/nbtvah9ek6azx2hgv0cd.png" alt="tut2.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519385238/nbtvah9ek6azx2hgv0cd.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519385238/nbtvah9ek6azx2hgv0cd.png 2x" /><br /> Here you must add opencv libraries from where you extracted the opencv folder, go to c/c++ -> general and in the additional include directories insert your opencv include address:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443676/qto3ycvh3qtgs3fkuzw4.png" alt="tut5.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443676/qto3ycvh3qtgs3fkuzw4.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443676/qto3ycvh3qtgs3fkuzw4.png 2x" /><br /> like: OPENCV_ADDRESS\build\include<br /> Next in linker -> General add Additional libraries directory like this:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443967/fgkqwm7srtltostejbol.png" alt="tut6.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443967/fgkqwm7srtltostejbol.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519443967/fgkqwm7srtltostejbol.png 2x" /><br /> The address is: OPENCV_ADDRESS\x64\vc14\lib <p dir="auto"><strong>Note: for Visual Studio 2015 you must select vc14<br /> Now from OPENCV_ADDRESS\build\x64\vc14\lib select <strong>opencv_world340d.lib and copy the name like the picture below:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519444385/efznjwfuzg1eack3raax.png" alt="tut9.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519444385/efznjwfuzg1eack3raax.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519444385/efznjwfuzg1eack3raax.png 2x" /><br /> Then in Visual Studio paste it in linker -> input -> Additional Dependencies:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519444459/mifghezkqor3cj8im37e.png" alt="tut7.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519444459/mifghezkqor3cj8im37e.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519444459/mifghezkqor3cj8im37e.png 2x" /><br /> We chose opencv_world340<strong>d.lib D for debug mode.<br /> Then press apply and Ok. <h2>Code Examples <p dir="auto">Now that our configuration is done in this first tutorial lets start with some basic Image processing operations using opencv libraries in c++ programming language.<br /> First you must include opencv libraries in the first line of your code: <pre><code>#include<opencv2/opencv.hpp> <p dir="auto"><strong>Read and Show Image <p dir="auto">For the first example we're going to read and show an image. so we also need iostream libraries and we should include it to our code: <pre><code>#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; void main() { Mat img = imread("1.jpg"); namedWindow("image", WINDOW_NORMAL); imshow("image", img); waitKey(0); } <p dir="auto">If you don't add this line of code: using namespace cv;<br /> Then you must use cv functions like this: cv::function. so in the above code we read an image using imread and show it using imshow and here is the result after build and running the code:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446494/rtejs8kgjjgqwluak6hc.png" alt="tut10.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446494/rtejs8kgjjgqwluak6hc.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446494/rtejs8kgjjgqwluak6hc.png 2x" /> <p dir="auto"><strong>Subtract <p dir="auto">Now i duplicated this image and made some changes on it to do subtract example:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446629/ta6qlsexbwdp0jffknba.png" alt="tut11.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446629/ta6qlsexbwdp0jffknba.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446629/ta6qlsexbwdp0jffknba.png 2x" /> <pre><code>#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; void main() { Mat img1 = imread("1.jpg"); Mat img2 = imread("2.jpg"); Mat result; subtract(img1, img2, result); namedWindow("image", WINDOW_NORMAL); imshow("image", result); waitKey(0); } <p dir="auto">And here is the result:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446770/mx7ljdkuzvudmn8o2e7z.png" alt="tut12.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446770/mx7ljdkuzvudmn8o2e7z.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519446770/mx7ljdkuzvudmn8o2e7z.png 2x" /> <p dir="auto"><strong>Resize <p dir="auto">Here is the resize example code: <pre><code>#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; void main() { Mat img1 = imread("1.jpg"); Size size(200, 200); Mat dest; resize(img1, dest, size); imshow("image1", img1); imshow("image2", dest); waitKey(0); } <p dir="auto">The result(original image size is 512*512):<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519449019/tkygerfdy8vncuxzwms9.png" alt="tut13.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519449019/tkygerfdy8vncuxzwms9.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519449019/tkygerfdy8vncuxzwms9.png 2x" /> <p dir="auto"><strong>Accessing pixel values <p dir="auto">In this example because the original picture size is too big, first we will resize it to 20 by 20 then we print the pixel values like this: <pre><code>#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; void main() { Mat img1 = imread("1.jpg"); Size size(10, 10); Mat dest; resize(img1, dest, size); imshow("image2", dest); print(dest); waitKey(0); } <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519449390/jmvm8hs5epd8hylloq2h.png" alt="tut14.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519449390/jmvm8hs5epd8hylloq2h.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519449390/jmvm8hs5epd8hylloq2h.png 2x" /> <p dir="auto"><strong>Change pixel values <p dir="auto">To change pixel values I provide an example here in this example first I get RGB color values for the blue sky in background like this:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519450608/bmrbej9pqamhb43ho16s.png" alt="tut15.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519450608/bmrbej9pqamhb43ho16s.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519450608/bmrbej9pqamhb43ho16s.png 2x" /><br /> Then I want to change the light blue colors to black color using this code: <pre><code>#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; void main() { Mat img = imread("1.jpg"); imshow("Image1", img); for (int y = 0; y<img.rows; y++) { for (int x = 0; x<img.cols; x++) { // get pixel Vec3b color = img.at<Vec3b>(Point(x, y)); if (color[0] >= 250 && color[1] >= 190 && color[2] >= 70) { color[0] = 0; color[1] = 0; color[2] = 0; } // set pixel img.at<Vec3b>(Point(x, y)) = color; } } imshow("Image2",img); waitKey(0); } <p dir="auto">Vec3b is the abbreviation for "vector with 3 byte entries" which is RGB but in BGR order so in the above example color[0] is blue. and here is the result: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519450850/erzz4fuakf59ljp57npt.png" alt="tut16.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519450850/erzz4fuakf59ljp57npt.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519450850/erzz4fuakf59ljp57npt.png 2x" /> <p dir="auto"><strong>Region of interest or ROI <p dir="auto">To select a ROI from original image we can do it like this: <pre><code>#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; void main() { Mat img = imread("1.jpg"); Rect roi = Rect(0, 0 , 150, 150); Mat roiImg(img, roi); imshow("Image1", img); imshow("Image2", roiImg); waitKey(0); } <p dir="auto">Rect(0, 0 , 150, 150); is Rect(X, Y, WITH, HEIGHT);<br /> And here is the result:<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519451922/cd6eqgnihznwoobaenao.png" alt="tut17.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519451922/cd6eqgnihznwoobaenao.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1519451922/cd6eqgnihznwoobaenao.png 2x" /> <h4>Curriculum <p dir="auto">This is my first tutorial contribution in Image processing using OpenCV, in next contributions I'm going to do better examples and maybe solve some Image processing problems. <p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@hadif66/tutorial-image-processing-operations-using-opencv-and-visual-studio" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Sort:  

Great dude, you can make it episodic and more project based, in a way that everyone can follow...

Thanks I'm actually looking for some image processing challenges which I may solve and share it here.

Alternatively you can also use python shell , install opencv , numpy and matplotlib to play around with images....

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @hadif66 I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thanks @hadif66! You solved my problem and clearly stated the procedure. Keep it up!