-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
983198c
commit 8f54eeb
Showing
44 changed files
with
931 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import cv2 | ||
|
||
def ReadVideo(videoPath): | ||
cap = cv2.VideoCapture(videoPath) | ||
#Read video frames until available | ||
while(cap.isOpened()): | ||
ret, frame = cap.read() | ||
if ret== True: | ||
#Quickly display image | ||
cv2.imshow('frame',frame) | ||
if cv2.waitKey(1) & 0xFF == ord('q'): | ||
break | ||
else: | ||
break | ||
# Release everything if job is finished | ||
cap.release() | ||
cv2.destroyAllWindows() | ||
|
||
#https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import cv2 | ||
def WriteVideo(videoPath): | ||
cap = cv2.VideoCapture(videoPath) | ||
# Define the codec and create VideoWriter object | ||
fourcc = cv2.VideoWriter_fourcc(*'XVID') | ||
out = cv2.VideoWriter('output.avi',fourcc, 47.95, (1280,720)) | ||
|
||
while(cap.isOpened()): | ||
ret, frame = cap.read() | ||
if ret==True: | ||
frame = (255-frame) #Invert | ||
# write the inversed frame | ||
out.write(frame) | ||
#Quickly display image using the image command | ||
cv2.imshow('frame',frame) | ||
if cv2.waitKey(1) & 0xFF == ord('q'): | ||
break | ||
else: | ||
break | ||
# Release everything if job is finished | ||
cap.release() | ||
out.release() | ||
cv2.destroyAllWindows() | ||
|
||
return out #(out,'output.avi') #could return both the output video and path | ||
|
||
'''import Page_4_Reading_Videos as readV #small test program | ||
WriteVideo("vtest.avi") | ||
readV.ReadVideo("output.avi")''' | ||
|
||
#https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html | ||
#It seems opencv is the main librray for video streaming, could not find for direct playing like implay on matlab |
Binary file added
BIN
+582 Bytes
ComputerVision/Week_10_11/__pycache__/Page_4_Reading_Videos.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#************* | ||
# Not Tested | ||
#************* | ||
import numpy as np | ||
|
||
def CalculateSimpleLUT(a): | ||
x = [i for i in range(256)] | ||
LUT = [ 255/(1+np.exp(-a*(j-127)/32)) for j in x] | ||
return LUT | ||
|
||
#OR | ||
def CalculateSimpleLUT_2(a): | ||
LUT = [ 255/(1+np.exp(-a*(x-127)/32)) for x in range(256)] | ||
return LUT |
19 changes: 19 additions & 0 deletions
19
ComputerVision/Week_2/Page_45_Multi-Band_Histogram_Calculator_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#************* | ||
# Not Tested | ||
#************* | ||
import numpy as np | ||
|
||
def histogram(I): #multiband histogram calculation | ||
|
||
h,w,d=I.shape | ||
|
||
hist=np.zeros([256,d],dtype=np.uint8) #allocate the histogram | ||
|
||
#range through the intensity values | ||
for g in range(256): | ||
hist[g][0] = sum([i.tolist().count(g) for i in I[:,:,0]]) #accumulate | ||
hist[g][1] = sum([i.tolist().count(g) for i in I[:,:,1]]) | ||
hist[g][2] = sum([i.tolist().count(g) for i in I[:,:,2]]) | ||
|
||
return hist | ||
|
43 changes: 43 additions & 0 deletions
43
ComputerVision/Week_2/Page_71_Histogram_Matching_Algorithm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
|
||
def HistogramMatch(I,J): | ||
'''I=cv2.imread("tony-stark.jpg",0) | ||
J=cv2.imread("thanos.jpg",0)''' | ||
|
||
'''cv2.imshow("Original Image",I) | ||
cv2.imshow("Target Image",J)''' | ||
|
||
Hi,Wi = I.shape | ||
Hj,Wj = J.shape | ||
|
||
K=np.zeros([Hi,Wi],dtype=np.uint8) | ||
|
||
mj=np.min(J) | ||
Mj=np.max(J) | ||
|
||
mi=np.min(I) | ||
Mi=np.max(I) | ||
|
||
PI_noncumulative,edges=np.histogram([a for a in I.ravel()],256,[0,256],[0,1]) | ||
PI=PI_noncumulative.cumsum() | ||
PJ_noncumulative,edges=np.histogram([a for a in J.ravel()],256,[0,256],[0,1]) | ||
PJ=PJ_noncumulative.cumsum() | ||
|
||
gj=mj | ||
for gi in range(mi,Mi): | ||
while gj<256 and PI[gi]<1 and PJ[gj]<PI[gi]: | ||
gj = gj + 1 | ||
K[I==gi]=gj | ||
|
||
'''cv2.imshow("Remapped Image",K) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows() ''' | ||
return K | ||
|
||
'''I=cv2.imread("tony-stark.jpg",0) #example program | ||
J=cv2.imread("thanos.jpg",0) | ||
cv2.imshow("Remapped Image", HistogramMatch(I,J)) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows() ''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
|
||
def ApplyLUTWithGamma(I,gamma): | ||
|
||
invGamma =gamma | ||
lut = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8") | ||
|
||
K= cv2.LUT(I, lut) #LUT mapping function of Python | ||
|
||
'''cv2.imshow("Original",I) #image showing | ||
cv2.imshow("K",K) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' | ||
return K | ||
|
||
'''I=cv2.imread("thanos.jpg",0) #example program | ||
cv2.imshow("K",ApplyLUTWithGamma(I,0.8)) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' | ||
|
||
# Sources: | ||
# www.learnopencv.com/applycolormap-for-pseudocoloring-in-opencv-c-python/ | ||
# www.programcreek.com/python/example/89460/cv2.LUT | ||
|
||
#ApplyLUTWithGamma("tony-stark.jpg",0.3) original run |
21 changes: 21 additions & 0 deletions
21
ComputerVision/Week_2/Page_78_Look_Up_Table_For_Histogram_Matching.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#************* | ||
# Not Tested | ||
#************* | ||
import numpy as np | ||
|
||
def CalculateLUT(I,J): | ||
|
||
PI_noncumulative,edges=np.histogram([a for a in I.ravel()],256,[0,256],[0,1]) | ||
PI=PI_noncumulative.cumsum() | ||
PJ_noncumulative,edges=np.histogram([a for a in J.ravel()],256,[0,256],[0,1]) | ||
PJ=PJ_noncumulative.cumsum() | ||
|
||
LUT = np.zeros([256]) | ||
gJ=0 | ||
|
||
for gI in range(256): | ||
while PJ[gJ] < PI[gI] and gJ<256: | ||
gJ = gJ+1 | ||
LUT[gI] = gJ | ||
|
||
return LUT |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import cv2 | ||
|
||
import numpy as np | ||
import math | ||
|
||
def cropBorders(img,tol=0): #https://codereview.stackexchange.com/questions/132914/crop-black-border-of-image-using-numpy answer by Divakar | ||
mask=img>0 | ||
return img[np.ix_(mask.any(1),mask.any(0))] | ||
|
||
def PlateBW(I): | ||
|
||
'''I=cv2.imread("LicensePlate.png",0) #read image | ||
cv2.imshow("License Plate Original", I)''' | ||
|
||
height,width=I.shape[:2] | ||
|
||
M= cv2.getRotationMatrix2D((width/2,height/2),-15,1) | ||
|
||
sin = math.sin(M[0,0]) | ||
cos = math.cos(M[0,1]) | ||
bound_w = int((height * abs(sin)) + (width * abs(cos))) | ||
bound_h = int((height * abs(cos)) + (width * abs(sin))) | ||
|
||
M[0, 2] += ((bound_w / 2) - width/2) | ||
M[1, 2] += ((bound_h / 2) - height/2) | ||
|
||
J1 = cv2.warpAffine(I,M,(bound_w,bound_h)) | ||
|
||
J1=cropBorders(J1) | ||
'''cv2.imshow("License Plate Rotated", J1)''' | ||
|
||
|
||
tform=np.float32([ [1,0.3,0],[0,1,0]]) | ||
|
||
|
||
h,w=J1.shape[:2] #height and width values of the image | ||
J2=np.zeros([h*2,w*2],dtype=np.uint8) | ||
|
||
J2 = cv2.warpAffine(J1,tform,(w*2,h*2)) | ||
J2=cropBorders(J2) | ||
|
||
'''cv2.imshow("License Plate Skewed", J2) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' | ||
return J2 | ||
|
||
|
||
#**************-color version | ||
def PlateColor(I): | ||
'''I=cv2.imread("LicensePlate.png") #read image | ||
cv2.imshow("License Plate Original", I)''' | ||
|
||
height,width=I.shape[:2] | ||
|
||
# Rotate clockwise 15 degrees to align base | ||
M= cv2.getRotationMatrix2D((width/2,height/2),-15,1) | ||
J1 = cv2.warpAffine(I,M,(width,height)) | ||
'''cv2.imshow("License Plate Rotated", J1)''' | ||
|
||
|
||
# Now apply a skew | ||
tform=np.float32([ [1,0.3,0],[0,1,0]]) | ||
height,width=J1.shape[:2] | ||
J2=np.zeros([height,width],dtype=np.uint8) | ||
J2 = cv2.warpAffine(J1,tform,(width,height)) | ||
'''cv2.imshow("License Plate Skewed", J2) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' | ||
return J2 | ||
|
||
|
||
'''I=cv2.imread("LicensePlate.png") #read image #small test program | ||
IBW=cv2.imread("LicensePlate.png",0) #read image | ||
cv2.imshow("BW", PlateBW(IBW)) | ||
cv2.imshow("Color", PlateColor(I)) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import cv2 | ||
|
||
|
||
def ReadGrayImage(imagePath): | ||
return cv2.imread(imagePath,0) | ||
|
||
'''cv2.imshow("Birds",ReadGrayImage("birds.png")) #example program | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
|
||
def ThresholdWithFunction(I,T): | ||
ret,B=cv2.threshold(I, T, 255, cv2.THRESH_BINARY_INV) | ||
return B | ||
|
||
def ThresholdWithBoolean(I,T): | ||
B = np.zeros((I.shape)) | ||
B[I<30] = 255 | ||
return B | ||
|
||
'''I = cv2.imread("birds.png",0) #example program | ||
T=30 | ||
cv2.imshow("Thresholded at 30 Function",ThresholdWithFunction(I,T)) | ||
cv2.imshow("Thresholded at 30 Boolean",ThresholdWithBoolean(I,T)) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' |
14 changes: 14 additions & 0 deletions
14
ComputerVision/Week_8/Page_11_Basic_Thresholding_Algorithm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#************* | ||
# Not Tested | ||
#************* | ||
import numpy as np | ||
|
||
def BasicThreshold(I,T): | ||
Y=0 | ||
X=0 | ||
T=0 | ||
B =np.zeros(I.shape,dtype=np.bool) | ||
|
||
for y in range(0,Y): | ||
for x in range(0,X): | ||
B[y][x]= (I[y][x]>=T) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
def Label(I): | ||
ret,B=cv2.threshold(I, 70, 255, cv2.THRESH_BINARY_INV) | ||
'''cv2.imshow("Thresholded at 70",B)''' | ||
J = cv2.morphologyEx(B, cv2.MORPH_OPEN, np.ones((3,3),np.uint8)) | ||
'''cv2.imshow('Opened',J)''' | ||
|
||
labelCount, labels = cv2.connectedComponents(J) | ||
# Map component labels to hue val | ||
label_hue = np.uint8(179*labels/np.max(labels)) | ||
blank_ch = 255*np.ones_like(label_hue) | ||
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch]) | ||
|
||
# cvt to BGR for display | ||
L = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR) | ||
|
||
# set bachground label to black | ||
L[label_hue==0] = 0 | ||
|
||
'''cv2.imshow('Labeled. Number of birds = ' + str(labelCount-1), L) #backgorund is also labeled | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' | ||
return (L,labelCount-1) | ||
|
||
#source: https://stackoverflow.com/questions/46441893/connected-component-labeling-in-python | ||
|
||
'''I = cv2.imread("birds.png",0) #example program | ||
(img,count)=Label(I) | ||
cv2.imshow('Labeled. Number of birds = ' + str(count), img) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import cv2 | ||
from matplotlib import pyplot as plt | ||
|
||
def Threshold(I,T): | ||
'''cv2.imshow("Lungs",I)''' | ||
|
||
plt.hist(I.ravel(),bins=255) | ||
plt.title("Histogram h(I)") | ||
'''plt.show()''' | ||
|
||
ret,B=cv2.threshold(I, T, 255, cv2.THRESH_BINARY) | ||
'''cv2.imshow("Thresholded at "+str(T),B) | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' | ||
return (B,plt) | ||
|
||
|
||
|
||
'''I = cv2.imread("lung.png",0) #example program | ||
T = 130 | ||
cv2.imshow("Thresholded at " + str(T),Threshold(I,T)[0]) | ||
Threshold(I,T)[1].show() | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows()''' |
Oops, something went wrong.