#2.1 import cv2 as cv dat = cv.imread( "sample.jpg" ) height, width, channel = dat.shape for y in range(height): for x in range(width): b = dat[y][x][0] g = dat[y][x][1] r = dat[y][x][2] a = (int(r)+int(g)+int(b))/3 if (a>=128): dat[y][x][0]=255 dat[y][x][1]=255 dat[y][x][2]=255 else: dat[y][x][0]=0 dat[y][x][1]=0 dat[y][x][2]=0 cv.imwrite( "output2_1.jpg", dat ) #2.2 import cv2 as cv dat = cv.imread( "sample.jpg" ) height, width, channel = dat.shape for y in range(height): for x in range(width): b = dat[y][x][0] g = dat[y][x][1] r = dat[y][x][2] a = (int(r)+int(g)+int(b))/3 dat[y][x][0]=a dat[y][x][1]=a dat[y][x][2]=a cv.imwrite( "output2_2.jpg", dat ) #2.3 import cv2 as cv dat = cv.imread( "sample.jpg" ) height, width, channel = dat.shape total=0 stotal=0 for y in range(height): for x in range(width): b = dat[y][x][0] g = dat[y][x][1] r = dat[y][x][2] total += int(r)+int(g)+int(b) stotal += int(r)**2+int(g)**2+int(b)**2 ave=total/height/width/3; var=stotal/height/width/3 - ave*ave print("平均値: ", ave, "分散:", var) #3.1 import cv2 as cv dat = cv.imread( "sample.jpg" ) height, width, channel = dat.shape for y in range(height): for x in range(width): b = dat[y][x][0] g = dat[y][x][1] r = dat[y][x][2] if (r==255 and g==255 and b==255) or (r==0 and g==0 and b==0): dat[y][x][2]=255 dat[y][x][1]=0 dat[y][x][0]=0 cv.imwrite( "output3_1.jpg", dat ) #3.2 import cv2 as cv dat = cv.imread( "sample.jpg" ) height, width, channel = dat.shape rtotal=0 gtotal=0 btotal=0 srtotal=0 sgtotal=0 sbtotal=0 for y in range(height): for x in range(width): b = dat[y][x][0] g = dat[y][x][1] r = dat[y][x][2] rtotal += int(r) gtotal += int(g) btotal += int(b) srtotal += int(r)**2 sgtotal += int(g)**2 sbtotal += int(b)**2 rave=rtotal/height/width; gave=gtotal/height/width; bave=btotal/height/width; rvar=srtotal/height/width - rave*rave gvar=sgtotal/height/width - gave*gave bvar=sbtotal/height/width - bave*bave print("R平均値: ", rave, "R分散:", rvar) print("G平均値: ", gave, "G分散:", gvar) print("B平均値: ", bave, "B分散:", bvar) #3.4 import cv2 as cv import math dat = cv.imread( "sample.jpg" ) height, width, channel = dat.shape #3.3の解答例の方法を実装する #画像の平均値をave, 分散をvarとする #明度値の分布が正規分布であると仮定し、正規分布表を #用いれば、±127.5の範囲に90%の明度値が分布する場合 #の明度値の標準偏差は、127.5/1.64=77.74である。 #これを用いてa,bは以下のように決定できる a=77.74/math.sqrt(var) b=127.5-ave for y in range(height): for x in range(width): for ch in range(channel): k=(int(dat[y][x][ch])-128)*a+128+b if (k>255): k=255 elif (k<0): k=0 dat[y][x][ch]=k cv.imwrite( "output3_4.jpg", dat ) #3.5 import cv2 as cv import numpy as np dat = cv.imread( "sample.jpg" ) height, width, channel = dat.shape # edge_x = dat.copy() などとしてもよい edge_h = np.zeros((height, width, 3), dtype="uint8") edge_v = np.zeros((height, width, 3), dtype="uint8") total=0 stotal=0 #ここではRGBそれぞれについて出力の絶対値を可視化している for y in range(1,height-1): for x in range(1,width-1): for ch in range(channel): edge_h[y][x][ch] = abs( int( dat[y][x+1][ch] )-int( dat[y][x-1][ch] ) ) edge_v[y][x][ch] = abs( int( dat[y+1][x][ch] )-int( dat[y-1][x][ch] ) ) cv.imwrite( "edge_h.jpg", edge_h ) cv.imwrite( "edge_v.jpg", edge_v ) #3.7 import cv2 as cv dat0 = cv.imread( "sample1.jpg" ) dat1 = cv.imread( "sample2.jpg" ) height, width, channel = dat1.shape ssd=0 for y in range(height): for x in range(width): for ch in range(channel): ssd+=(int(dat0[y][x][ch])-int(dat1[y][x][ch]))**2 print( "SSD:", ssd )