Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from __future__ import division
- import cv2
- import matplotlib.colors
- import numpy as np
- import argparse
- from matplotlib import pyplot as plt
- import matplotlib
- from PIL import Image
- from numpy import random
- def rgb2yuv(rgb_arr):
- (row, col, _) = rgb_arr.shape
- arr_yuv = np.zeros([row,col,3], dtype=np.uint8)
- for i in range(0,row):
- for j in range(0,col):
- 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
- 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
- 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
- return arr_yuv
- def hist_plot(img):
- # empty list to store the count
- # of each intensity value
- count = []
- # empty list to store intensity
- # value
- r = []
- # loop to traverse each intensity
- # value
- for k in range(0, 256):
- r.append(k)
- count1 = 0
- # loops to traverse each pixel in
- # the image
- for i in range(m):
- for j in range(n):
- if img[i, j] == k:
- count1 += 1
- count.append(count1)
- return (r, count)
- # parser = argparse.ArgumentParser(description='Code for Histogram Calculation tutorial.')
- # parser.add_argument('--input', help='Path to input image.', default='.jpg')
- # args = parser.parse_args()
- #
- image = cv2.imread("name.png",cv2.IMREAD_COLOR)
- histSize = 256
- histRange = (0, 256)
- accumulate = False
- hist_h,hist_w =image.shape[:2]
- bin_w = int(round( hist_w/histSize ))
- histImage = np.zeros((hist_h, hist_w, 3), np.uint8)
- #
- # # b- 0 g- 1 r -2
- # red = image.copy()
- # red[:,:,0]=red[:,:,1]=0
- #
- #
- # green = image.copy()
- # green[:,:,0]=green[:,:,2]=0
- #
- # blue = image.copy()
- # blue[:,:,1]=blue[:,:,2]=0
- # B, G, R = cv2.split(image)
- #
- # #RGB NA YUV
- # image1 = Image.open("obrazek.jpg")
- # (w,h)=image1.size
- # arr_rgb = np.array(list(image1.getdata())).astype(np.uint8).reshape(h,w,3)
- #
- #
- # arr_yuv_cal = rgb2yuv(arr_rgb)
- # arr_ycal = np.zeros([h,w], dtype=np.uint8)
- # arr_ycal[:,:]=arr_yuv_cal[:,:,0]
- #
- #
- #
- # #Grayscale
- # img1 = mpimg.imread("obrazek.jpg")
- # Re,Ge,Bl = img1[:,:,0],img1[:,:,1],img1[:,:,2]
- # imgGray = 0.2898 * Re + 0.5870 * Ge + 0.1140* Bl
- #
- #
- # #Grayscale from AVG
- # image2=cv2.imread("obrazek.jpg",cv2.IMREAD_COLOR)
- # h1,w1 = image2.shape[:2]
- # for i in range(0,h1):
- # for j in range(0,w1):
- # b,g,r = image2[i,j]
- # image2[i,j]=((int(r)+int(g)+int(b))/3)
- #
- # #Grayscale lightness
- # image3=cv2.imread("obrazek.jpg",cv2.IMREAD_COLOR)
- # h2,w2,_ = image3.shape[:3]
- # for i in range(0,h2):
- # for j in range(0,w2):
- # b1,g1,r1=image3[i,j]
- # image3[i,j]=(int(max(r1,g1,b1))+int(min(r1,g1,b1)))/2
- #
- # #Grayscale luminosity
- # image4=cv2.imread("obrazek.jpg",cv2.IMREAD_COLOR)
- # h3,w3,_=image3.shape[:3]
- # for i in range(0,h3):
- # for j in range(0,w3):
- # b2,g2,r2=image4[i,j]
- # image4[i,j]=0.21*r2+0.72*g2+0.07*b2
- #Y from YUV cv2
- img = Image.open("obrazek.jpg")
- (w,h) = img.size
- arr_rgb = np.array(list(img.getdata())).astype(np.uint8).reshape(h, w, 3)
- arr_y = np.zeros([h, w], dtype=np.uint8)
- arr_yuv_cal = rgb2yuv(arr_rgb)
- arr_ycal = np.zeros([h, w], dtype=np.uint8)
- arr_ycal[:, :] = arr_yuv_cal[:, :, 0]
- y,u,v=cv2.split(image);
- ###!!! coś nie pykło
- #yuv=Image.fromarray((result*255).astype(np.uint8))
- # b_hist = cv2.calcHist(B, [0], None, [256],[0,256]) #wyliczenie historgramu blue
- # g_hist = cv2.calcHist(G, [1], None, [256],[0,256]) #wyliczenie histogramu green
- # r_hist = cv2.calcHist(R, [2], None, [256],[0,256]) #wyliczenie histogramu red
- y_hist = cv2.calcHist(y, [0], None, [256],[0,256]) #wyliczenie historgramu y
- u_hist = cv2.calcHist(u, [1], None, [256],[0,256]) #wyliczenie histogramu u
- v_hist = cv2.calcHist(v, [2], None, [256],[0,256]) #wyliczenie histogramu v
- # #!!!!!KIEDY UZYJE blue || green || red wyrzuca maly histogram
- #
- #
- #
- #
- cv2.normalize(y_hist,y_hist,0,hist_h,cv2.NORM_MINMAX) #eliminacja zlych charakterystyk, podniesienie jakosci pixela
- cv2.normalize(u_hist,u_hist,0,hist_h,cv2.NORM_MINMAX)#eliminacja zlych charakterystyk, podniesienie jakosci pixela
- cv2.normalize(v_hist,v_hist,0,hist_h,cv2.NORM_MINMAX)#eliminacja zlych charakterystyk, podniesienie jakosci pixela
- for i in range(1, histSize):
- cv2.line(histImage, ( bin_w*(i-1), hist_h - int(y_hist[i-1]) ),
- ( bin_w*(i), hist_h - int(y_hist[i]) ),
- ( 255, 0, 0), 2)
- cv2.line(histImage, ( bin_w*(i-1), hist_h - int(u_hist[i-1]) ),
- ( bin_w*(i), hist_h - int(u_hist[i]) ),
- ( 0, 255, 0), 2)
- cv2.line(histImage, ( bin_w*(i-1), hist_h - int(v_hist[i-1]) ),
- ( bin_w*(i), hist_h - int(v_hist[i]) ),
- ( 0, 0, 255), 2)
- #Histogram
- cv2.imshow('Source image', image)
- cv2.imshow('calcHist Demo', histImage)
- #Pojedyncze kolory
- # cv2.imshow("blue",blue)
- # cv2.imshow("red",red)
- # cv2.imshow("green",green)
- #Grayscale w okreslonej skali
- # plt.imshow(imgGray,cmap='gray')
- #Grayscale AVG
- # cv2.imshow("Grayscale AVG",image2)
- # #Gryscale lightness
- # cv2.imshow("Grayscale lighntess",image3)
- # #Grayscale luminosity
- # cv2.imshow("Grayscale luminosity",image4)
- #Y
- #img_yuv=cv2.imread("safdsaf.jpg",cv2.COLOR_RGB2YUV)
- #cv2.imshow("yuv2",img_yuv)
- #yuv.show()
- # binaryzacja
- ## pobierz piksel z obrazu
- image2=cv2.imread("name.png",cv2.IMREAD_GRAYSCALE)
- h3,w3=image2.shape[:2]
- img_thres=np.zeros((h3,w3))
- for i in range(0,h3):
- for x in range(0,w3):
- pixel = image2[i,x]
- img_thres[i,x]=0 if pixel<128 else pixel
- cv2.imshow("bin",img_thres)
- #YUV w okreslonej skali
- plt.imshow(arr_ycal)
- matplotlib.image.imsave('name.png',arr_ycal)
- cv2.imshow('y',y)
- cv2.imshow('u',u)
- cv2.imshow('v',v)
- #wyswietlenie plota
- plt.show()
- #Wyswietlenie pojedynczych kolorow
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement