|
| 1 | +//Exercises_9_5.cpp Exercises at end of Chapter 9 |
| 2 | +#include <opencv2/opencv.hpp> |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +using namespace cv; |
| 6 | +using namespace std; |
| 7 | + |
| 8 | + |
| 9 | +void help(const char **argv) { |
| 10 | + cout << "\n\n" |
| 11 | + << "This program solves the Exercise 4、5 at the end of Chapter 9 \n" |
| 12 | + << "Call:\n" |
| 13 | + << argv[0] << " <path/image_name1>" << " <path/image_name2>\n\n" |
| 14 | + << "For example: ./" << argv[0] << " ../left.jpg "<< " ../left.jpg\n" |
| 15 | + << endl; |
| 16 | +} |
| 17 | + |
| 18 | +int main( int argc, const char** argv ) |
| 19 | +{ |
| 20 | + help(argv); |
| 21 | + if(argc < 3) { |
| 22 | + cout << "\nERROR: You had too few parameters.\n" << endl; |
| 23 | + return -1; |
| 24 | + } |
| 25 | + Mat temp; |
| 26 | + Mat temp2; |
| 27 | + /************************************************************************/ |
| 28 | + /* 5. Create an outline of an object. Take a picture of a scene. Then, without moving |
| 29 | + the camera, put a coffee cup in the scene and take a second picture. Load these |
| 30 | + images and convert both to 8-bit grayscale images. |
| 31 | + a. Take the absolute value of their difference. Display the result, which should |
| 32 | + look like a noisy mask of a coffee mug. |
| 33 | + b. Do a binary threshold of the resulting image using a level that preserves most |
| 34 | + of the coffee mug but removes some of the noise. Display the result. The “on” |
| 35 | + values should be set to 255. |
| 36 | + c. Do a cv::MOP_OPEN on the image to further clean up noise. |
| 37 | + d. Using the erosion operator and logical XOR function, turn the mask of the |
| 38 | + coffee cup image into an outline of the coffee cup (only the edge pixels |
| 39 | + remaining). */ |
| 40 | + /************************************************************************/ |
| 41 | + Mat matMug = imread(argv[1],IMREAD_GRAYSCALE); |
| 42 | + Mat matNoMug = imread(argv[2],IMREAD_GRAYSCALE); |
| 43 | + if (matMug.empty() || matNoMug.empty()) |
| 44 | + { |
| 45 | + cout << "\nERROR: parameters is not a image name.\n" << endl; |
| 46 | + return -1; |
| 47 | + } |
| 48 | + //a |
| 49 | + absdiff(matMug,matNoMug,temp); |
| 50 | + imshow("absolute value of mug ",temp); |
| 51 | + //b |
| 52 | + threshold(temp,temp,100,255,THRESH_OTSU); |
| 53 | + imshow("a binary threshold",temp); |
| 54 | + //c |
| 55 | + morphologyEx(temp,temp,cv::MORPH_OPEN,Mat()); |
| 56 | + imshow("MORPH_OPEN",temp); |
| 57 | + //d |
| 58 | + erode(temp,temp,Mat()); |
| 59 | + matMug.copyTo(temp2,temp); |
| 60 | + imshow("an outline of the coffee cup",temp2); |
| 61 | + waitKey(); |
| 62 | + return 0; |
| 63 | + |
| 64 | +} |
0 commit comments