Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. velo = cv2.imread("velo.jpg")
  5. cosmo = cv2.imread("cosmo.jpg")
  6. cheshir = cv2.imread("cot.jpg")
  7. furie = cv2.imread("detail.jpg", cv2.IMREAD_GRAYSCALE)
  8. people = cv2.imread("people.jpg")
  9. fotik = cv2.imread("fotik.jpg")
  10. n = input("Какой номер показываем? ")
  11. if n == "1":
  12. cheshir_canny = cheshir.copy()
  13. highTh = 50
  14. lowTh1 = highTh / 1.5
  15. cheshir_canny1 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
  16. lowTh2 = highTh / 2.75
  17. cheshir_canny2 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
  18. lowTh3 = highTh / 4
  19. cheshir_canny3 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
  20. cv2.imshow("original", cheshir)
  21. cv2.imshow("canny 50 1.5", cheshir_canny1)
  22. cv2.imshow("canny 50 2.75", cheshir_canny2)
  23. cv2.imshow("canny 50 4", cheshir_canny3)
  24. highTh = 75
  25. lowTh1 = highTh / 1.5
  26. cheshir_canny4 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
  27. lowTh2 = highTh / 2.75
  28. cheshir_canny5 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
  29. lowTh3 = highTh / 4
  30. cheshir_canny6 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
  31. cv2.imshow("canny 75 1.5", cheshir_canny4)
  32. cv2.imshow("canny 75 2.75", cheshir_canny5)
  33. cv2.imshow("canny 75 4", cheshir_canny6)
  34. highTh = 125
  35. lowTh1 = highTh / 1.5
  36. cheshir_canny7 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
  37. lowTh2 = highTh / 2.75
  38. cheshir_canny8 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
  39. lowTh3 = highTh / 4
  40. cheshir_canny9 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
  41. cv2.imshow("canny 125 1.5", cheshir_canny7)
  42. cv2.imshow("canny 125 2.75", cheshir_canny8)
  43. cv2.imshow("canny 125 4", cheshir_canny9)
  44. highTh = 175
  45. lowTh1 = highTh / 1.5
  46. cheshir_canny10 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
  47. lowTh2 = highTh / 2.75
  48. cheshir_canny11 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
  49. lowTh3 = highTh / 4
  50. cheshir_canny12 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
  51. cv2.imshow("canny 175 1.5", cheshir_canny10)
  52. cv2.imshow("canny 175 2.75", cheshir_canny11)
  53. cv2.imshow("canny 175 4", cheshir_canny12)
  54. highTh = 225
  55. lowTh1 = highTh / 1.5
  56. cheshir_canny13 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
  57. lowTh2 = highTh / 2.75
  58. cheshir_canny14 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
  59. lowTh3 = highTh / 4
  60. cheshir_canny15 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
  61. cv2.imshow("canny 225 1.5", cheshir_canny10)
  62. cv2.imshow("canny 225 2.75", cheshir_canny11)
  63. cv2.imshow("canny 225 4", cheshir_canny12)
  64. cv2.waitKey(0)
  65. cv2.destroyAllWindows()
  66. if n == "2":
  67. gray = cv2.cvtColor(velo, cv2.COLOR_BGR2GRAY)
  68. edges = cv2.Canny(gray, 50, 150, apertureSize=3)
  69. cv2.imshow('edges', edges)
  70.  
  71. lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
  72. print(lines)
  73. for line in lines:
  74. rho, theta = line[0]
  75. a = np.cos(theta)
  76. b = np.sin(theta)
  77. x0 = a * rho
  78. y0 = b * rho
  79. # x1 stores the rounded off value of (r * cos(theta) - 1000 * sin(theta))
  80. x1 = int(x0 + 2000 * (-b))
  81. # y1 stores the rounded off value of (r * sin(theta)+ 1000 * cos(theta))
  82. y1 = int(y0 + 2000 * a)
  83. # x2 stores the rounded off value of (r * cos(theta)+ 1000 * sin(theta))
  84. x2 = int(x0 - 2000 * (-b))
  85. # y2 stores the rounded off value of (r * sin(theta)- 1000 * cos(theta))
  86. y2 = int(y0 - 2000 * a)
  87. cv2.line(velo, (x1, y1), (x2, y2), (0, 0, 255), 2)
  88.  
  89. cv2.imshow('image', velo)
  90.  
  91. gray_img = cv2.cvtColor(cosmo, cv2.COLOR_BGR2GRAY)
  92. img = cv2.medianBlur(gray_img, 5)
  93. cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  94.  
  95. # center
  96.  
  97. circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 120, param1=100, param2=30, minRadius=0, maxRadius=0)
  98. circles = np.uint16(np.around(circles))
  99.  
  100. for i in circles[0, :]:
  101. cv2.circle(cosmo, (i[0], i[1]), i[2], (0, 255, 0), 6)
  102. cv2.circle(cosmo, (i[0], i[1]), 2, (0, 0, 255), 3)
  103.  
  104. cv2.imshow("HoughCirlces", cosmo)
  105. cv2.waitKey(0)
  106. cv2.destroyAllWindows()
  107. if n == "3":
  108. cv2.imshow("orig", furie)
  109. fft_image = np.fft.fft2(furie)
  110. fft_shift_image = np.fft.fftshift(fft_image)
  111.  
  112. magnitude_spectrum = 20 * np.log(np.abs(fft_shift_image))
  113. magnitude_spectrum = np.asarray(magnitude_spectrum, dtype=np.uint8)
  114.  
  115. normal = np.random.randint(-4, 6, 1)
  116.  
  117. # Esli vidast 0+0j proga budet lomat'sya
  118.  
  119. fft_normal = np.fft.fft(normal)
  120.  
  121. multi = fft_normal * fft_image
  122.  
  123. inverse_multi = np.fft.ifft2(multi)
  124.  
  125. magnitude_spectrum_inverse_muliti = 20 * np.log((np.abs(inverse_multi) + 1))
  126. magnitude_spectrum_inverse_muliti = np.asarray(magnitude_spectrum_inverse_muliti, dtype=np.uint8)
  127.  
  128. speech = "inverse fft with: " + str(fft_normal)
  129. cv2.imshow(speech, magnitude_spectrum_inverse_muliti)
  130.  
  131. cv2.imshow("fft", magnitude_spectrum)
  132. cv2.waitKey(0)
  133. cv2.destroyAllWindows()
  134.  
  135. if n == "4":
  136. img = cv2.imread("scene.jpg", cv2.IMREAD_GRAYSCALE)
  137. img1 = img.copy()
  138. cv2.imshow("original", img1)
  139. print(img.shape)
  140.  
  141. intIMAGE = cv2.integral(img)
  142. MaxFormatValue = 255
  143. koeff = MaxFormatValue / intIMAGE[intIMAGE.shape[0] - 1][intIMAGE.shape[1] - 1]
  144.  
  145. integral = intIMAGE * (koeff)
  146. npINTEG = np.asarray(integral)
  147.  
  148. print("Viberite oblast izobrazheniya:")
  149. x = int(input())
  150. y = int(input())
  151. diff_image = cv2.Sobel(npINTEG[x:, :y], -1, 1, 1)
  152.  
  153. cv2.imshow("original1212", diff_image)
  154. cv2.waitKey(0)
  155. cv2.destroyAllWindows()
  156.  
  157. if n == "5":
  158. image = cv2.imread("ugol.jpg", cv2.IMREAD_GRAYSCALE)
  159. print(image.shape)
  160.  
  161. print("x1,y1,x2,y2:")
  162. y1 = int(input())
  163. x1 = int(input())
  164. y2 = int(input())
  165. x2 = int(input())
  166. new_image = image[y1:y2, x1:x2]
  167. Non, Non, titled = cv2.integral3(new_image)
  168. cv2.imshow("new image with new x and y", new_image)
  169.  
  170. integral_45 = titled
  171.  
  172. print(integral_45)
  173.  
  174. MaxFormatValue = 255
  175. koeff = MaxFormatValue / integral_45[integral_45.shape[0] - 1][integral_45.shape[1] - 1]
  176. print(koeff)
  177.  
  178. integral_45 = integral_45 * (koeff)
  179.  
  180. print(integral_45)
  181. cv2.imshow("int", integral_45)
  182.  
  183. cv2.waitKey(0)
  184. cv2.destroyAllWindows()
  185.  
  186. if n == "6":
  187. cv2.imshow("original", people)
  188. people1 = people.copy()
  189. gray = cv2.cvtColor(people, cv2.COLOR_BGR2GRAY)
  190. blur = cv2.GaussianBlur(gray, (1, 1), 1000)
  191. flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
  192. # Find contours
  193. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  194. contours = sorted(contours, key=cv2.contourArea, reverse=True)
  195. # Select long perimeters only
  196. perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
  197. listindex = [i for i in range(15) if perimeters[i] > perimeters[0] / 2]
  198. numcards = len(listindex)
  199. # Show image
  200. imgcont = people.copy()
  201. [cv2.drawContours(imgcont, [contours[i]], 0, (255, 255, 255), 5) for i in listindex]
  202. # cv2.imshow("contur", imgcont)
  203.  
  204. mask = np.zeros(people.shape[: 2], np.uint8)
  205. mask1 = people - imgcont
  206. r, thresh1 = cv2.threshold(mask1, 128, 255, cv2.THRESH_BINARY)
  207. # cv2.imshow("mask", thresh1)
  208.  
  209. bgdModel = np.zeros((1, 65), np.float64)
  210. fgdModel = np.zeros((1, 65), np.float64)
  211. # mask[mask1 == 0] = 0
  212. # mask[mask1 == 255] = 1
  213. rect = (460, 10, 1200, 800)
  214. cv2.grabCut(people, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
  215.  
  216. mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype("uint8")
  217.  
  218. people = people * mask2[:, :, np.newaxis]
  219.  
  220. cv2.imshow("catting", people)
  221.  
  222. fon = people1 - people
  223. cv2.imshow("catting1", fon)
  224. dst1 = cv2.cvtColor(people, cv2.COLOR_BGR2GRAY)
  225. r, g, b = cv2.split(people)
  226. dst = cv2.inpaint(fon, r, 3, cv2.INPAINT_TELEA)
  227. cv2.imshow("paint", dst)
  228. cv2.waitKey(0)
  229. cv2.destroyAllWindows()
  230.  
  231. if n == "7":
  232. masked = np.zeros(fotik.shape[: 2], np.uint8)
  233. # r, g, b = cv2.split(fotik)
  234. img1 = cv2.pyrMeanShiftFiltering(src=fotik, sp=20, sr=45, dst=3)
  235. #img1 = cv2.floodFill(img)
  236. #cv2.imshow("seff", img1)
  237. cv2.imshow("original", fotik)
  238. cv2.imshow("seg1", img1)
  239. cv2.waitKey(0)
  240. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement