# koniec

May 21st, 2022
654
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from __future__ import print_function
2. from __future__ import division
3. import cv2
4. import matplotlib.colors
5. import numpy as np
6. import argparse
7. from matplotlib import pyplot as plt
8. import matplotlib
9. from PIL import Image
10. from numpy import random
11.
12. def rgb2yuv(rgb_arr):
13.     (row, col, _) = rgb_arr.shape
14.     arr_yuv = np.zeros([row,col,3], dtype=np.uint8)
15.
16.     for i in range(0,row):
17.         for j in range(0,col):
18.             arr_yuv[i][j][0] = (rgb_arr[i][j][0]*299 + rgb_arr[i][j][1]*587 + rgb_arr[i][j][2]*114 + 500) / 1000
19.             arr_yuv[i][j][1]=(rgb_arr[i][j][0]*-0.16874 + rgb_arr[i][j][1]*-0.33126 + rgb_arr[i][j][2]*0.50000 + 500) / 1000
20.             arr_yuv[i][j][2] = (rgb_arr[i][j][0] * 0.50000 + rgb_arr[i][j][1] * -0.41869 + rgb_arr[i][j][2] * -0.08131 + 500) / 1000
21.     return arr_yuv
22.
23.
24. def hist_plot(img):
25.     # empty list to store the count
26.     # of each intensity value
27.     count = []
28.
29.     # empty list to store intensity
30.     # value
31.     r = []
32.
33.     # loop to traverse each intensity
34.     # value
35.     for k in range(0, 256):
36.         r.append(k)
37.         count1 = 0
38.
39.         # loops to traverse each pixel in
40.         # the image
41.         for i in range(m):
42.             for j in range(n):
43.                 if img[i, j] == k:
44.                     count1 += 1
45.         count.append(count1)
46.
47.     return (r, count)
48.
49.
50.
51.
52.
53.
54.
55. # parser = argparse.ArgumentParser(description='Code for Histogram Calculation tutorial.')
56. # parser.add_argument('--input', help='Path to input image.', default='.jpg')
57. # args = parser.parse_args()
58. #
60. histSize = 256
61. histRange = (0, 256)
62. accumulate = False
63. hist_h,hist_w =image.shape[:2]
64. bin_w = int(round( hist_w/histSize ))
65. histImage = np.zeros((hist_h, hist_w, 3), np.uint8)
66. #
67. # # b- 0 g- 1 r -2
68. # red = image.copy()
69. # red[:,:,0]=red[:,:,1]=0
70. #
71. #
72. # green = image.copy()
73. # green[:,:,0]=green[:,:,2]=0
74. #
75. # blue = image.copy()
76. # blue[:,:,1]=blue[:,:,2]=0
77. # B, G, R = cv2.split(image)
78. #
79. # #RGB NA YUV
80. # image1 = Image.open("obrazek.jpg")
81. # (w,h)=image1.size
82. # arr_rgb = np.array(list(image1.getdata())).astype(np.uint8).reshape(h,w,3)
83. #
84. #
85. # arr_yuv_cal = rgb2yuv(arr_rgb)
86. # arr_ycal = np.zeros([h,w], dtype=np.uint8)
87. # arr_ycal[:,:]=arr_yuv_cal[:,:,0]
88. #
89. #
90. #
91. # #Grayscale
93. # Re,Ge,Bl = img1[:,:,0],img1[:,:,1],img1[:,:,2]
94. # imgGray = 0.2898 * Re + 0.5870 * Ge + 0.1140* Bl
95. #
96. #
97. # #Grayscale from AVG
99. # h1,w1 = image2.shape[:2]
100. # for i in range(0,h1):
101. #     for j in range(0,w1):
102. #         b,g,r = image2[i,j]
103. #         image2[i,j]=((int(r)+int(g)+int(b))/3)
104. #
105. # #Grayscale lightness
107. # h2,w2,_ = image3.shape[:3]
108. # for i in range(0,h2):
109. #     for j in range(0,w2):
110. #          b1,g1,r1=image3[i,j]
111. #          image3[i,j]=(int(max(r1,g1,b1))+int(min(r1,g1,b1)))/2
112. #
113. # #Grayscale luminosity
115. # h3,w3,_=image3.shape[:3]
116. # for i in range(0,h3):
117. #     for j in range(0,w3):
118. #         b2,g2,r2=image4[i,j]
119. #         image4[i,j]=0.21*r2+0.72*g2+0.07*b2
120.
121. #Y from YUV cv2
122. img = Image.open("obrazek.jpg")
123. (w,h) = img.size
124.
125. arr_rgb = np.array(list(img.getdata())).astype(np.uint8).reshape(h, w, 3)
126. arr_y = np.zeros([h, w], dtype=np.uint8)
127. arr_yuv_cal = rgb2yuv(arr_rgb)
128. arr_ycal = np.zeros([h, w], dtype=np.uint8)
129. arr_ycal[:, :] = arr_yuv_cal[:, :, 0]
130.
131.
132. y,u,v=cv2.split(image);
133.
134.
135. ###!!! coś nie pykło
136. #yuv=Image.fromarray((result*255).astype(np.uint8))
137.
138.
139.
140. # b_hist = cv2.calcHist(B, [0], None, [256],[0,256]) #wyliczenie historgramu blue
141. # g_hist = cv2.calcHist(G, [1], None, [256],[0,256]) #wyliczenie histogramu green
142. # r_hist = cv2.calcHist(R, [2], None, [256],[0,256]) #wyliczenie histogramu red
143.
144. y_hist = cv2.calcHist(y, [0], None, [256],[0,256]) #wyliczenie historgramu y
145. u_hist = cv2.calcHist(u, [1], None, [256],[0,256]) #wyliczenie histogramu u
146. v_hist = cv2.calcHist(v, [2], None, [256],[0,256]) #wyliczenie histogramu v
147.
148.
149.
150.
151. # #!!!!!KIEDY UZYJE blue || green || red wyrzuca maly histogram
152. #
153. #
154. #
155. #
156. cv2.normalize(y_hist,y_hist,0,hist_h,cv2.NORM_MINMAX) #eliminacja zlych charakterystyk, podniesienie jakosci pixela
157. cv2.normalize(u_hist,u_hist,0,hist_h,cv2.NORM_MINMAX)#eliminacja zlych charakterystyk, podniesienie jakosci pixela
158. cv2.normalize(v_hist,v_hist,0,hist_h,cv2.NORM_MINMAX)#eliminacja zlych charakterystyk, podniesienie jakosci pixela
159.
160. for i in range(1, histSize):
161.     cv2.line(histImage, ( bin_w*(i-1), hist_h - int(y_hist[i-1]) ),
162.             ( bin_w*(i), hist_h - int(y_hist[i]) ),
163.             ( 255, 0, 0), 2)
164.     cv2.line(histImage, ( bin_w*(i-1), hist_h - int(u_hist[i-1]) ),
165.             ( bin_w*(i), hist_h - int(u_hist[i]) ),
166.             ( 0, 255, 0), 2)
167.     cv2.line(histImage, ( bin_w*(i-1), hist_h - int(v_hist[i-1]) ),
168.             ( bin_w*(i), hist_h - int(v_hist[i]) ),
169.             ( 0, 0, 255), 2)
170.
171. #Histogram
172. cv2.imshow('Source image', image)
173. cv2.imshow('calcHist Demo', histImage)
174. #Pojedyncze kolory
175. # cv2.imshow("blue",blue)
176. # cv2.imshow("red",red)
177. # cv2.imshow("green",green)
178. #Grayscale w okreslonej skali
179. # plt.imshow(imgGray,cmap='gray')
180. #Grayscale AVG
181. # cv2.imshow("Grayscale AVG",image2)
182. # #Gryscale lightness
183. # cv2.imshow("Grayscale lighntess",image3)
184. # #Grayscale luminosity
185. # cv2.imshow("Grayscale luminosity",image4)
186.
187. #Y
188.
190. #cv2.imshow("yuv2",img_yuv)
191. #yuv.show()
192.
193.
194.
195.
196.
197. # binaryzacja
198. ## pobierz piksel z obrazu
200.
201. h3,w3=image2.shape[:2]
202.
203. img_thres=np.zeros((h3,w3))
204.
205. for i in range(0,h3):
206.     for x in range(0,w3):
207.         pixel = image2[i,x]
208.         img_thres[i,x]=0 if pixel<128 else pixel
209.
210. cv2.imshow("bin",img_thres)
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228. #YUV w okreslonej skali
229. plt.imshow(arr_ycal)
230.
231. matplotlib.image.imsave('name.png',arr_ycal)
232.
233. cv2.imshow('y',y)
234. cv2.imshow('u',u)
235. cv2.imshow('v',v)
236.
237.
238.
239.
240. #wyswietlenie plota
241. plt.show()
242.
243.
244. #Wyswietlenie pojedynczych kolorow
245. cv2.waitKey(0)
246. cv2.destroyAllWindows()
247.