Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. #include "opencv2/imgproc.hpp"
  4. #include "opencv2/imgcodecs.hpp"
  5. #include "opencv2/videoio.hpp"
  6. #include "opencv2/highgui.hpp"
  7. #include "cstdlib"
  8.  
  9. using namespace std;
  10. using namespace cv;
  11.  
  12. Mat image, image2;
  13. Mat src1, src2;
  14. int klik = 0;
  15.  
  16. int progR = 127;
  17. int progG = 127;
  18. int progB = 127;
  19. int alpha_slider = 127; // Initialize values
  20. int alpha_slider2 = 127; // Initialize values
  21. int alpha_slider3 = 127; // Initialize values
  22. int alpha_slider_max = 255;
  23. char TrackbarName[50];
  24. char TrackbarName2[50];
  25. char TrackbarName3[50];
  26.  
  27. Mat tlo;
  28. Mat tv;
  29. vector <Point2f> narozniki4;
  30. vector<Point2f> Cel;
  31. Mat lambda(2, 4, CV_32FC1);
  32. Mat output;
  33.  
  34.  
  35. void NegativeDirectMemoryAccess(Mat& img, Mat& img2)
  36. {
  37. img2 = img.clone();
  38. /*int rows = img.rows;
  39. int cols = img.cols;
  40. for (int i = 0; i < rows; ++i)
  41. {
  42. for (int j = 0; j < cols; ++j)
  43. {
  44. if ((img.at<uchar>(i, j) % 2) == 1) img2.at<uchar>(i, j) = 0;
  45. else img2.at<uchar>(i, j) = 255;
  46. }
  47. }*/
  48.  
  49. int numberOfChannels = img.channels();
  50. int numberOfPixels = img.rows * img.cols * numberOfChannels;
  51. uchar* p = img2.data;
  52. for (int i = 0; i < numberOfPixels; i = i + numberOfChannels)
  53. {
  54. for (int j = 0; j < numberOfChannels; ++j)
  55. {
  56. if ((p[i + j] % 2) == 1) p[i + j] = 255;
  57. else p[i + j] = 0;
  58. }
  59. }
  60. }
  61.  
  62.  
  63. static void onMouse(int event, int x, int y, int, void*)
  64. {
  65. if (event == EVENT_RBUTTONDOWN)
  66. {
  67. cout << "[" << x << "," << y << "]" << endl;
  68. }
  69. if (event == EVENT_LBUTTONDOWN)
  70. {
  71. Point temp1;
  72. temp1 = Point(x, y);
  73. Cel.push_back(temp1);
  74. klik++;
  75.  
  76. if (klik >= 4)
  77. {
  78. klik = 0;
  79. cout << "Punkty celu:\n" << Cel << endl;
  80.  
  81. lambda = Mat::zeros(tv.rows, tv.cols, tv.type());
  82. lambda = getPerspectiveTransform(narozniki4 ,Cel);
  83. warpPerspective(tv, output, lambda, Size(tlo.cols, tlo.rows)); //rozmiar obrazu wyjscowego
  84.  
  85. namedWindow("output"); // Create Windows
  86. imshow("output", output);
  87.  
  88. int numberOfChannels = output.channels();
  89. int numberOfPixels = output.rows * output.cols * numberOfChannels;
  90. uchar* p = output.data;
  91. uchar* wyjscie = tlo.data;
  92. for (int i = 0; i < numberOfPixels; i = i + numberOfChannels)
  93. {
  94. for (int j = 0; j < numberOfChannels; ++j)
  95. {
  96. if (p[i + j] != 0) wyjscie[i + j] = p[i + j];
  97. }
  98. }
  99. imshow("output2", tlo);
  100.  
  101. //Load two images
  102. //img1 = cv2.imread('messi5.jpg')
  103. //img2 = cv2.imread('opencv-logo.png')
  104. // I want to put logo on top - left corner, So I create a ROI
  105. //rows, cols, channels = img2.shape;
  106. //roi = img1[0:rows, 0 : cols];
  107. // Now create a mask of logo and create its inverse mask also
  108. //img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  109. //mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
  110. //mask_inv = cv2.bitwise_not(mask)
  111. //Now black - out the area of logo in ROI
  112. //img1_bg = cv2.bitwise_and(roi, roi, mask = mask_inv)
  113. //Take only region of logo from logo image.
  114. //img2_fg = cv2.bitwise_and(img2, img2, mask = mask)
  115. // Put logo in ROI and modify the main image
  116. //dst = cv2.add(img1_bg, img2_fg)
  117. //img1[0:rows, 0 : cols] = dst
  118. //cv2.imshow('res', img1)
  119.  
  120. /*Mat roi = tlo.clone();
  121. Mat tvgrey, mask, mask_inv, img1_bg, img2_fg;
  122. cvtColor(tv, tvgrey, COLOR_BGR2GRAY);
  123. threshold(tvgrey, mask, 10, 255, THRESH_BINARY);
  124. bitwise_not(mask, mask_inv);
  125. bitwise_and(roi,roi, img1_bg, mask = mask_inv);
  126. bitwise_and(tv, tv, img2_fg, mask = mask);
  127. add(img1_bg, img2_fg, output);
  128.  
  129. imshow("output2", tlo);*/
  130.  
  131. }
  132.  
  133. }
  134. }
  135.  
  136. int main(int argc, char** argv)
  137. {
  138. //zad.3
  139. /*src1 = imread("zad_3.png", IMREAD_GRAYSCALE);
  140. if (!src1.data) { printf("Error loading src1 \n"); return -1; }
  141. imshow("org", src1);
  142. NegativeDirectMemoryAccess(src1, src2);
  143. imshow("dekodowany", src2);*/
  144.  
  145. //zad.1
  146. tlo = imread("zad_1.jpg");
  147. if (!tlo.data) { printf("Error loading src1 \n"); return -1; }
  148. imshow("Tlo", tlo);
  149. tv = imread("foto.jpg");
  150. if (!tv.data) { printf("Error loading src1 \n"); return -1; }
  151. //imshow("Tv", tv);
  152. setMouseCallback("Tlo", onMouse, 0);
  153.  
  154. Point2f temporary;
  155. temporary.x = 0;
  156. temporary.y = 0;
  157. narozniki4.push_back(temporary);
  158. temporary.x = tv.cols;
  159. temporary.y = 0;
  160. narozniki4.push_back(temporary);
  161. temporary.x = tv.cols;
  162. temporary.y = tv.rows;
  163. narozniki4.push_back(temporary);
  164. temporary.x = 0;
  165. temporary.y = tv.rows;
  166. narozniki4.push_back(temporary);
  167. cout << "Narozniki :\n" << narozniki4 << endl;
  168.  
  169. //zad.2
  170.  
  171. /*namedWindow("org"); // Create Windows
  172. image = imread("zad_2.jpg", CV_LOAD_IMAGE_COLOR); // Read the file
  173. imshow("org", image);
  174. sprintf_s(TrackbarName, "B %d", alpha_slider_max);
  175. sprintf_s(TrackbarName2, "G %d", alpha_slider_max);
  176. sprintf_s(TrackbarName3, "R %d", alpha_slider_max);
  177. createTrackbar(TrackbarName, "org", &alpha_slider, alpha_slider_max, on_trackbar);
  178. on_trackbar(alpha_slider, 0);
  179. createTrackbar(TrackbarName2, "org", &alpha_slider2, alpha_slider_max, on_trackbar2);
  180. on_trackbar2(alpha_slider2, 0);
  181. createTrackbar(TrackbarName3, "org", &alpha_slider3, alpha_slider_max, on_trackbar3);
  182. on_trackbar3(alpha_slider3, 0);
  183.  
  184. progowanie(image, image2);
  185. imshow("progowanie", image2);*/
  186.  
  187.  
  188.  
  189.  
  190.  
  191. waitKey(0);
  192.  
  193. }
  194.  
  195.  
  196.  
  197. void progowanie(Mat& _src, Mat& _dst) //progowanie
  198. {
  199. _src.copyTo(_dst);
  200. typedef Vec<uchar, 3> Pixel;
  201.  
  202. for (int i = 0; i < _src.rows; i++)
  203. {
  204. for (int j = 0; j < _src.cols; j++)
  205. {
  206. //zielony
  207. if (_dst.at<Pixel>(i, j).val[1] > progG) _dst.at<Pixel>(i, j).val[1] = 255;
  208. else _dst.at<Pixel>(i, j).val[1] = 0;
  209.  
  210.  
  211. //niebieski
  212. //if (_dst.at<Pixel>(i, j).val[0] > progB) _dst.at<Pixel>(i, j).val[0] = 255;
  213. //else _dst.at<Pixel>(i, j).val[0] = 0;
  214.  
  215. //czerwony
  216. if (_dst.at<Pixel>(i, j).val[2] > progR) _dst.at<Pixel>(i, j).val[2] = 255;
  217. else _dst.at<Pixel>(i, j).val[2] = 0;
  218. }
  219. }
  220.  
  221.  
  222. }
  223.  
  224. void on_trackbar(int, void*)
  225. {
  226. progB = alpha_slider;
  227. progowanie(image, image2);
  228. imshow("progowanie", image2);
  229.  
  230. }
  231.  
  232. void on_trackbar2(int, void*)
  233. {
  234. progG = alpha_slider;
  235. progowanie(image, image2);
  236. imshow("progowanie", image2);
  237.  
  238. }
  239.  
  240. void on_trackbar3(int, void*)
  241. {
  242. progR = alpha_slider;
  243. progowanie(image, image2);
  244. imshow("progowanie", image2);
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement