Skip to content

Commit c6dd9ca

Browse files
authored
Add files via upload
1 parent 3c6039d commit c6dd9ca

33 files changed

+1657
-0
lines changed

AddNoise.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# We will add salt & pepper noise on an image.
2+
# Noises are unwanted signals.
3+
4+
import cv2
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
import random
8+
9+
def main():
10+
11+
img_path = "D:\\Dataset\\4.1.03.tiff"
12+
img = cv2.imread(img_path, 1) # takes img as bgr
13+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
14+
15+
rows, columns, channels = img.shape # returns 3 values which is about the given image.
16+
p = 0.02 # If we reduce this value, noises will become less than before on the image.
17+
output = np.zeros(img.shape, dtype = np.uint8)
18+
19+
print(rows) # output will be : 256
20+
print(columns) # output will be : 256
21+
print(channels) # output will be : 3
22+
23+
for i in range(rows):
24+
for j in range(columns):
25+
r = random.random() # creates a random value is named r
26+
if r < p/2:
27+
output[i][j] = [0, 0, 0] #pepper noise sprinkles as 0
28+
elif r < p:
29+
output[i][j] = [255, 255, 255] #salt noise sprinkles as 1
30+
else:
31+
output[i][j] = img[i][j]
32+
33+
plt.imshow(output)
34+
plt.title('Image with "Salt & Pepper" Noise')
35+
plt.show()
36+
37+
#cv2.imshow('Result', img)
38+
#cv2.waitKey(0)
39+
#cv2.destroyAllWindows()
40+
41+
if __name__ == '__main__':
42+
main()

HoughLine.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#HIGH PASS FILTERS can perform these actions:
2+
# edge detection --> using Hough Line Transform
3+
4+
import cv2
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
8+
9+
def main():
10+
11+
windowName = 'Hough Line Transform Method'
12+
13+
cap = cv2.VideoCapture(0)
14+
15+
if cap.isOpened():
16+
ret, frame = cap.read()
17+
else:
18+
ret = False
19+
20+
while ret:
21+
22+
ret, frame = cap.read()
23+
24+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
25+
edges = cv2.Canny(gray, 50, 250, apertureSize=3, L2gradient=True)
26+
27+
lines = cv2.HoughLines(edges, 1, np.pi/180, 150)
28+
29+
if lines is not None:
30+
for rho, theta in lines[0]:
31+
a = np.cos(theta)
32+
b = np.sin(theta)
33+
x0 = a*rho
34+
y0 = b*rho
35+
pts1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
36+
pts2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
37+
cv2.line(frame, pts1, pts2, (0, 255, 0), 3)
38+
39+
cv2.imshow(windowName, frame)
40+
41+
if cv2.waitKey(1)==27: # Exit on ESC
42+
break
43+
44+
cap.release()
45+
cv2.destroyAllWindows()
46+
47+
48+
if __name__ == '__main__':
49+
main()
50+

ImgSegment06.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import cv2
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
def main():
6+
7+
path = "C:\\Users\\enesa\\Documents\\MATLAB\\blobs_objects.jpg"
8+
9+
img = cv2.imread(path, 1)
10+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
11+
12+
filter1 = np.array(([0, -1, 0], [-1, 5, -1], [0, -1, 0]), np.float32) #Sharpening Filter
13+
output = cv2.filter2D(img, -1, filter1) #convolution filter
14+
15+
blur = cv2.GaussianBlur(img,(5,5),0)
16+
17+
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
18+
19+
_, thresh = cv2.threshold(gray,170,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
20+
21+
###########################################################################################################################
22+
23+
# Create a simple filter. The kernel slides through the image (as in 2D convolution).
24+
kernel = np.ones((3, 3), np.uint8)
25+
26+
# Create a Rectangular Structuring Element
27+
se1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
28+
29+
# Create a Elliptical Structuring Element
30+
se2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
31+
32+
# Apply Erosion method over the image with kernel
33+
erosion = cv2.erode(thresh,se1,iterations = 1)
34+
35+
# Apply Dilation method over the image with kernel
36+
dilation = cv2.dilate(thresh,se2,iterations = 1)
37+
38+
# Noise removal using Morphological closing operation
39+
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel, iterations = 4)
40+
41+
# Noise removal using Morphological opening operation
42+
opening = cv2.morphologyEx(erosion, cv2.MORPH_OPEN, kernel, iterations = 1)
43+
44+
###########################################################################################################################
45+
46+
dilation = 255 - dilation # Complementing Operation
47+
48+
_, contours, _ = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
49+
50+
print("{} Objects have detected!".format(len(contours)))
51+
52+
original = cv2.imread(path, 1)
53+
54+
original = cv2.cvtColor(original, cv2.COLOR_BGR2RGB)
55+
56+
sayac = 0
57+
for i in contours:
58+
# perimeter = cv2.arcLength(i,True)
59+
# if perimeter > 20:
60+
sayac = sayac +1
61+
#cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
62+
x,y,w,h = cv2.boundingRect(i)
63+
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
64+
cv2.putText(img, str(sayac), (x+10, y+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
65+
66+
#plt.plot(cx, cy, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=12) # belilenen noktaya x isareti koy.
67+
#cv2.putText(img, 'x', (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
68+
#cv2.putText(closing, str(sayac), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1)
69+
70+
print("{} Objects have drown!".format(sayac))
71+
72+
###########################################################################################################################
73+
# output = [original, img]
74+
# titles = ['Original', 'Contours']
75+
76+
77+
# for i in range(2):
78+
# plt.subplot(1, 2, i+1)
79+
# plt.imshow(output[i])
80+
# plt.title(titles[i])
81+
# plt.xticks([])
82+
# plt.yticks([])
83+
84+
cv2.imshow('Orignal Image', img)
85+
#cv2.imshow('Erosion Image', erosion)
86+
cv2.imshow('Dilation Image', dilation)
87+
cv2.imshow('Closing Image', closing)
88+
cv2.imshow('Opening Image', opening)
89+
90+
91+
plt.show()
92+
cv2.waitKey(0)
93+
cv2.destroyAllWindows()
94+
95+
if __name__ == "__main__":
96+
main()

ImgSegment07.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import cv2
2+
import numpy as np
3+
import imutils
4+
5+
def nothing(x):
6+
pass
7+
8+
# Load an image
9+
path = "C:\\Users\\enesa\\Documents\\MATLAB\\final3.PNG"
10+
img = cv2.imread(path, 1)
11+
12+
# Resize The image
13+
# if img.shape[1] > 600:
14+
# img = imutils.resize(img, width=600)
15+
16+
if img.shape[0] > 400:
17+
img = imutils.resize(img, height=500)
18+
19+
# Create a window
20+
cv2.namedWindow('Finding Treshold')
21+
22+
# create trackbars for treshold change
23+
cv2.createTrackbar('Treshold','Finding Treshold', 0, 255, nothing)
24+
25+
26+
while(1):
27+
28+
# Clone original image to not overlap drawings
29+
clone = img.copy()
30+
31+
# Convert to gray
32+
gray = cv2.cvtColor(clone, cv2.COLOR_BGR2GRAY)
33+
34+
# get current positions of four trackbars
35+
r = cv2.getTrackbarPos('Treshold','Finding Treshold')
36+
37+
# Thresholding the gray image
38+
ret,thresh = cv2.threshold(gray, r, 255, cv2.THRESH_BINARY)
39+
40+
# To remove the noise in the image with a 5x5 Gaussian filter.
41+
blur = cv2.GaussianBlur(thresh, (5, 5), 0)
42+
43+
# Detect edges
44+
edges = cv2.Canny(blur, 50, 150)
45+
46+
# Find contours
47+
_, contours, _= cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
48+
49+
contour_list = []
50+
51+
for i in contours:
52+
area = cv2.contourArea(i)
53+
if (area > 30):
54+
contour_list.append(i)
55+
56+
# Draw contours on the original image
57+
cv2.drawContours(clone, contour_list, -1, (255,0,0), 2)
58+
59+
60+
#Displaying the results
61+
cv2.imshow('Detecting Objects', clone)
62+
cv2.imshow("Treshholding", thresh)
63+
64+
# ESC to break
65+
k = cv2.waitKey(1) & 0xFF
66+
if k == 27:
67+
break
68+
69+
# close all open windows
70+
cv2.destroyAllWindows()

OpenCamera.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#HIGH PASS FILTERS can perform these actions:
2+
# edge detection --> using Hough Line Transform
3+
# This code only for opening Camera
4+
5+
6+
import cv2
7+
#import numpy as np
8+
#import matplotlib.pyplot as plt
9+
10+
11+
def main():
12+
13+
windowName = 'Hough Line Transform'
14+
15+
cap = cv2.VideoCapture(0)
16+
17+
if cap.isOpened():
18+
ret, frame = cap.read()
19+
else:
20+
ret = False
21+
22+
while ret:
23+
24+
ret, frame = cap.read()
25+
26+
27+
cv2.imshow(windowName, frame)
28+
29+
if cv2.waitKey(1)==27:
30+
break
31+
32+
cv2.destroyAllWindows()
33+
cap.release()
34+
35+
if __name__ == '__main__':
36+
main()
37+

RemoveNoise.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# We will see the Median Filter is pretty useful to remove salt and pepper noise from an image.
2+
3+
import cv2
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
import random
7+
8+
def main():
9+
10+
img_path = "D:\\Dataset\\coins.png"
11+
img = cv2.imread(img_path, 1) # takes img as bgr
12+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
13+
14+
output = np.zeros(img.shape, dtype = np.uint8) # Creates a zeros matrix the same size img.shape for applying noise.
15+
p = 0.2
16+
17+
for i in range(img.shape[0]): # img.shape[0] shows the row of the image
18+
for j in range(img.shape[1]): # img.shape[1] shows the column of the image
19+
r = random.random()
20+
if r < p/2:
21+
output[i][j] = [0, 0, 0] #pepper noise sprinkles
22+
elif r < p:
23+
output[i][j] = [255, 255, 255] #salt noise sprinkles
24+
else:
25+
output[i][j] = img[i][j]
26+
27+
28+
cv2.imshow('Noisy Image', output)
29+
cv2.waitKey(0)
30+
cv2.destroyAllWindows()
31+
32+
kernel33 = np.ones((3,3), np.float32)/9 # 3-by-3 neighborhood avarage filter
33+
34+
average = cv2.filter2D(output, -1, kernel33) # Apply the average filter to remove noises.
35+
gaussian = cv2.GaussianBlur(output, (3,3), 0)
36+
median = cv2.medianBlur(output, 3)
37+
bilateral = cv2.bilateralFilter(output, 9, 75, 75)
38+
39+
outputs = [img, output, average, gaussian, median, bilateral]
40+
41+
titles = ['Original', 'Noisy', 'AverageFilter', 'GaussianBlur', 'MedianBlur', 'BilateralFilter']
42+
43+
for i in range(6):
44+
plt.subplot(2, 3, i+1)
45+
plt.imshow(outputs[i])
46+
plt.title(titles[i])
47+
plt.xticks([])
48+
plt.yticks([])
49+
50+
plt.show()
51+
52+
if __name__ == '__main__':
53+
main()

Segmentation01.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#Image Segmentation01
2+
3+
import cv2
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
8+
def main():
9+
10+
img_path = "C:\\Users\\enesa\\Documents\\MATLAB\\filtresim2.png"
11+
img = cv2.imread(img_path, 1) # takes img as bgr
12+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
13+
14+
cv2.imshow('Image', img)
15+
16+
17+
# hist = cv2.calcHist([img],[0],None,[256],[0,256])
18+
# plt.hist(img.ravel(),256,[0,256])
19+
# plt.title('Histogram for Image')
20+
21+
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
22+
cv2.imshow('Tresholding Image', thresh1)
23+
24+
plt.subplot(1, 2, 1)
25+
plt.imshow(img)
26+
plt.title('Original Image')
27+
28+
plt.subplot(1, 2, 2)
29+
plt.imshow(thresh1)
30+
plt.title('Tresholding Image = 100')
31+
32+
plt.show()
33+
cv2.waitKey(0)
34+
cv2.destroyAllWindows()
35+
36+
if __name__ == '__main__':
37+
main()

0 commit comments

Comments
 (0)