Skip to content

Commit 4322abc

Browse files
committed
Mar 10, 2021: 1st commit
1 parent dad4937 commit 4322abc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3855
-172
lines changed

Ostu/ostu_example.py

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import cv2
2-
import numpy as np
3-
from matplotlib import pyplot as plt
4-
5-
img = cv2.imread('img.png',0)
6-
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
7-
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
8-
blur = cv2.GaussianBlur(img,(5,5),0)
9-
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
10-
11-
titles = ['orgianl Noisy Image','Histogram','Global Threshold(v=127)',
12-
'orgianl Noisy Image','Histogram',"Otsu's THresholding",
13-
'Guassian filtered Image','Histogram',"Otsu's Thresholding"]
14-
images = [img,0,th1,
15-
img,0,th2,
16-
blur,0,th3]
17-
18-
for i in range(3):
19-
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
20-
plt.title(titles[i*3]),plt.xticks([]),plt.yticks([])
21-
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
22-
plt.title(titles[i*3+1]),plt.xticks([]),plt.yticks([])
23-
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
24-
plt.title(titles[i*3+2]),plt.xticks([]),plt.yticks([])
1+
import cv2
2+
import numpy as np
3+
from matplotlib import pyplot as plt
4+
5+
img = cv2.imread('img.png',0)
6+
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
7+
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
8+
blur = cv2.GaussianBlur(img,(5,5),0)
9+
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
10+
11+
titles = ['orgianl Noisy Image','Histogram','Global Threshold(v=127)',
12+
'orgianl Noisy Image','Histogram',"Otsu's THresholding",
13+
'Guassian filtered Image','Histogram',"Otsu's Thresholding"]
14+
images = [img,0,th1,
15+
img,0,th2,
16+
blur,0,th3]
17+
18+
for i in range(3):
19+
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
20+
plt.title(titles[i*3]),plt.xticks([]),plt.yticks([])
21+
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
22+
plt.title(titles[i*3+1]),plt.xticks([]),plt.yticks([])
23+
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
24+
plt.title(titles[i*3+2]),plt.xticks([]),plt.yticks([])
2525
plt.show()

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# image_processing_algo
2-
3-
In the project, we implemented the image processing methods
4-
* chap3_image_transform spatial_filter
5-
auto-contrast, histogram matching, spatial filters such as Gauss, Laplace, LoG, the weighted median
6-
* chap4_freq_filter
7-
2d DFT, 2d DCT based on FFT, filters in the frequency domain
1+
# image_processing_algo
2+
3+
In the project, we implemented the image processing methods
4+
* chap3_image_transform spatial_filter
5+
auto-contrast, histogram matching, spatial filters such as Gauss, Laplace, LoG, the weighted median
6+
* chap4_freq_filter
7+
2d DFT, 2d DCT based on FFT, filters in the frequency domain

harris_corner/Corner_Detection.py

+141-141
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,141 @@
1-
# 1st version written by Shivam Chourey
2-
# Zhankun corrected the definition of Sobel operators, and changed the "CornerStrengthThreshold"
3-
# Implementation of Harris Corner detection algorithm
4-
# This algoruthm is very useful in corner detection and is used in a number of applications
5-
# It's also used in algorithms like FAST and ORB(which uses FAST and BREIF)
6-
7-
import numpy as np
8-
import cv2
9-
10-
11-
12-
# Kernel operation using input operator of size 3*3
13-
def GetSobel(image, Sobel, width, height):
14-
# Initialize the matrix
15-
I_d = np.zeros((width, height), np.float32)
16-
17-
# For every pixel in the image
18-
for rows in range(width):
19-
for cols in range(height):
20-
# Run the Sobel kernel for each pixel
21-
if rows >= 1 or rows <= width-2 and cols >= 1 or cols <= height-2:
22-
for ind in range(3):
23-
for ite in range(3):
24-
I_d[rows][cols] += Sobel[ind][ite] * image[rows - ind - 1][cols - ite - 1]
25-
else:
26-
I_d[rows][cols] = image[rows][cols]
27-
28-
return I_d
29-
30-
31-
# Method implements the Harris Corner Detection algorithm
32-
def HarrisCornerDetection(image):
33-
34-
# The two Sobel operators - for x and y direction
35-
SobelX = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
36-
SobelY = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
37-
38-
w, h = image.shape
39-
40-
# X and Y derivative of image using Sobel operator
41-
ImgX = GetSobel(image, SobelX, w, h)
42-
ImgY = GetSobel(image, SobelY, w, h)
43-
44-
# # Eliminate the negative values
45-
# There are multiple ways this can be done
46-
# 1. Off setting with a positive value (commented out below)
47-
# 2. Setting negative values to Zero (commented out)
48-
# 3. Multiply by -1 (implemented below, found most reliable method)
49-
# ImgX += 128.0
50-
# ImgY += 128.0
51-
for ind1 in range(w):
52-
for ind2 in range(h):
53-
if ImgY[ind1][ind2] < 0:
54-
ImgY[ind1][ind2] *= -1
55-
# ImgY[ind1][ind2] = 0
56-
if ImgX[ind1][ind2] < 0:
57-
ImgX[ind1][ind2] *= -1
58-
# ImgX[ind1][ind2] = 0
59-
60-
# # Display the output results after Sobel operations
61-
# cv2.imshow("SobelX", ImgX)
62-
# cv2.imshow("SobelY", ImgY)
63-
64-
ImgX_2 = np.square(ImgX)
65-
ImgY_2 = np.square(ImgY)
66-
67-
ImgXY = np.multiply(ImgX, ImgY)
68-
ImgYX = np.multiply(ImgY, ImgX)
69-
70-
#Use Gaussian Blur
71-
Sigma = 1.4
72-
kernelsize = (3, 3)
73-
74-
ImgX_2 = cv2.GaussianBlur(ImgX_2, kernelsize, Sigma)
75-
ImgY_2 = cv2.GaussianBlur(ImgY_2, kernelsize, Sigma)
76-
ImgXY = cv2.GaussianBlur(ImgXY, kernelsize, Sigma)
77-
ImgYX = cv2.GaussianBlur(ImgYX, kernelsize, Sigma)
78-
# print(ImgXY.shape, ImgYX.shape)
79-
80-
alpha = 0.06
81-
R = np.zeros((w, h), np.float32)
82-
# For every pixel find the corner strength
83-
for row in range(w):
84-
for col in range(h):
85-
M_bar = np.array([[ImgX_2[row][col], ImgXY[row][col]], [ImgYX[row][col], ImgY_2[row][col]]])
86-
R[row][col] = np.linalg.det(M_bar) - (alpha * np.square(np.trace(M_bar)))
87-
return R
88-
89-
if __name__ == "__main__":
90-
#### Main Program ####
91-
firstimagename = "checkerboard.png"
92-
93-
# Get the first image
94-
bgr = cv2.imread(firstimagename)
95-
firstimage = cv2.cvtColor(bgr, cv2.COLOR_RGB2GRAY)
96-
w, h = firstimage.shape
97-
98-
# Covert image to color to draw colored circles on it
99-
bgr = cv2.cvtColor(firstimage, cv2.COLOR_GRAY2RGB)
100-
101-
# Corner detection
102-
R = HarrisCornerDetection(firstimage)
103-
104-
# Empirical Parameter
105-
# This parameter will need tuning based on the use-case
106-
CornerStrengthThreshold = 20000
107-
108-
# Plot detected corners on image
109-
radius = 1
110-
color = (0, 255, 0) # Green
111-
thickness = 1
112-
113-
PointList = []
114-
# Look for Corner strengths above the threshold
115-
for row in range(w):
116-
for col in range(h):
117-
if R[row][col] > CornerStrengthThreshold:
118-
# print(R[row][col])
119-
max = R[row][col]
120-
121-
# Local non-maxima suppression
122-
skip = False
123-
for nrow in range(5):
124-
for ncol in range(5):
125-
if row + nrow - 2 < w and col + ncol - 2 < h:
126-
if R[row + nrow - 2][col + ncol - 2] > max:
127-
skip = True
128-
break
129-
130-
if not skip:
131-
# Point is expressed in x, y which is col, row
132-
cv2.circle(bgr, (col, row), radius, color, thickness)
133-
PointList.append((row, col))
134-
135-
# Display image indicating corners and save it
136-
cv2.imshow("Corners", bgr)
137-
outname = "Output_" + str(CornerStrengthThreshold) + ".png"
138-
cv2.imwrite(outname, bgr)
139-
140-
cv2.waitKey(0)
141-
cv2.destroyAllWindows()
1+
# 1st version written by Shivam Chourey
2+
# Zhankun corrected the definition of Sobel operators, and changed the "CornerStrengthThreshold"
3+
# Implementation of Harris Corner detection algorithm
4+
# This algoruthm is very useful in corner detection and is used in a number of applications
5+
# It's also used in algorithms like FAST and ORB(which uses FAST and BREIF)
6+
7+
import numpy as np
8+
import cv2
9+
10+
11+
12+
# Kernel operation using input operator of size 3*3
13+
def GetSobel(image, Sobel, width, height):
14+
# Initialize the matrix
15+
I_d = np.zeros((width, height), np.float32)
16+
17+
# For every pixel in the image
18+
for rows in range(width):
19+
for cols in range(height):
20+
# Run the Sobel kernel for each pixel
21+
if rows >= 1 or rows <= width-2 and cols >= 1 or cols <= height-2:
22+
for ind in range(3):
23+
for ite in range(3):
24+
I_d[rows][cols] += Sobel[ind][ite] * image[rows - ind - 1][cols - ite - 1]
25+
else:
26+
I_d[rows][cols] = image[rows][cols]
27+
28+
return I_d
29+
30+
31+
# Method implements the Harris Corner Detection algorithm
32+
def HarrisCornerDetection(image):
33+
34+
# The two Sobel operators - for x and y direction
35+
SobelX = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
36+
SobelY = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
37+
38+
w, h = image.shape
39+
40+
# X and Y derivative of image using Sobel operator
41+
ImgX = GetSobel(image, SobelX, w, h)
42+
ImgY = GetSobel(image, SobelY, w, h)
43+
44+
# # Eliminate the negative values
45+
# There are multiple ways this can be done
46+
# 1. Off setting with a positive value (commented out below)
47+
# 2. Setting negative values to Zero (commented out)
48+
# 3. Multiply by -1 (implemented below, found most reliable method)
49+
# ImgX += 128.0
50+
# ImgY += 128.0
51+
for ind1 in range(w):
52+
for ind2 in range(h):
53+
if ImgY[ind1][ind2] < 0:
54+
ImgY[ind1][ind2] *= -1
55+
# ImgY[ind1][ind2] = 0
56+
if ImgX[ind1][ind2] < 0:
57+
ImgX[ind1][ind2] *= -1
58+
# ImgX[ind1][ind2] = 0
59+
60+
# # Display the output results after Sobel operations
61+
# cv2.imshow("SobelX", ImgX)
62+
# cv2.imshow("SobelY", ImgY)
63+
64+
ImgX_2 = np.square(ImgX)
65+
ImgY_2 = np.square(ImgY)
66+
67+
ImgXY = np.multiply(ImgX, ImgY)
68+
ImgYX = np.multiply(ImgY, ImgX)
69+
70+
#Use Gaussian Blur
71+
Sigma = 1.4
72+
kernelsize = (3, 3)
73+
74+
ImgX_2 = cv2.GaussianBlur(ImgX_2, kernelsize, Sigma)
75+
ImgY_2 = cv2.GaussianBlur(ImgY_2, kernelsize, Sigma)
76+
ImgXY = cv2.GaussianBlur(ImgXY, kernelsize, Sigma)
77+
ImgYX = cv2.GaussianBlur(ImgYX, kernelsize, Sigma)
78+
# print(ImgXY.shape, ImgYX.shape)
79+
80+
alpha = 0.06
81+
R = np.zeros((w, h), np.float32)
82+
# For every pixel find the corner strength
83+
for row in range(w):
84+
for col in range(h):
85+
M_bar = np.array([[ImgX_2[row][col], ImgXY[row][col]], [ImgYX[row][col], ImgY_2[row][col]]])
86+
R[row][col] = np.linalg.det(M_bar) - (alpha * np.square(np.trace(M_bar)))
87+
return R
88+
89+
if __name__ == "__main__":
90+
#### Main Program ####
91+
firstimagename = "checkerboard.png"
92+
93+
# Get the first image
94+
bgr = cv2.imread(firstimagename)
95+
firstimage = cv2.cvtColor(bgr, cv2.COLOR_RGB2GRAY)
96+
w, h = firstimage.shape
97+
98+
# Covert image to color to draw colored circles on it
99+
bgr = cv2.cvtColor(firstimage, cv2.COLOR_GRAY2RGB)
100+
101+
# Corner detection
102+
R = HarrisCornerDetection(firstimage)
103+
104+
# Empirical Parameter
105+
# This parameter will need tuning based on the use-case
106+
CornerStrengthThreshold = 20000
107+
108+
# Plot detected corners on image
109+
radius = 1
110+
color = (0, 255, 0) # Green
111+
thickness = 1
112+
113+
PointList = []
114+
# Look for Corner strengths above the threshold
115+
for row in range(w):
116+
for col in range(h):
117+
if R[row][col] > CornerStrengthThreshold:
118+
# print(R[row][col])
119+
max = R[row][col]
120+
121+
# Local non-maxima suppression
122+
skip = False
123+
for nrow in range(5):
124+
for ncol in range(5):
125+
if row + nrow - 2 < w and col + ncol - 2 < h:
126+
if R[row + nrow - 2][col + ncol - 2] > max:
127+
skip = True
128+
break
129+
130+
if not skip:
131+
# Point is expressed in x, y which is col, row
132+
cv2.circle(bgr, (col, row), radius, color, thickness)
133+
PointList.append((row, col))
134+
135+
# Display image indicating corners and save it
136+
cv2.imshow("Corners", bgr)
137+
outname = "Output_" + str(CornerStrengthThreshold) + ".png"
138+
cv2.imwrite(outname, bgr)
139+
140+
cv2.waitKey(0)
141+
cv2.destroyAllWindows()

sift/CMakeLists.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
project(extract_contour)
3+
4+
# set(CMAKE_BUILD_TYPE Release)
5+
# set(CMAKE_CXX_FLAGS "-std=c++14 -O3")
6+
# list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
7+
8+
# OpenCV
9+
find_package(OpenCV REQUIRED)
10+
# include_directories(${OpenCV_INCLUDE_DIRS}) # we can use this statement as well
11+
include_directories(OpenCV_INCLUDE_DIRS)
12+
13+
add_executable(sift sift.cpp)
14+
target_link_libraries(sift ${OpenCV_LIBS})
15+
set( CMAKE_INSTALL_BINDIR "${CMAKE_SOURCE_DIR}/bin/")
16+
install(TARGETS sift
17+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
18+
)
19+
20+
add_executable(surf surf.cpp)
21+
target_link_libraries(surf ${OpenCV_LIBS})
22+
set( CMAKE_INSTALL_BINDIR "${CMAKE_SOURCE_DIR}/bin/")
23+
install(TARGETS surf
24+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
25+
)
26+
27+
28+
# commands for compile to "./build"" folder, and install to "./bin" folder:
29+
# mkdir build
30+
# cd ./build
31+
# cmake ..
32+
# make
33+
# make install

sift/bin/sift

133 KB
Binary file not shown.

sift/bin/sift_match.png

1.31 MB
Loading

sift/bin/sift_match_1+keypoint.png

706 KB
Loading

sift/bin/sift_match_1.png

247 KB
Loading

sift/bin/sift_match_2+keypoint.png

620 KB
Loading

sift/bin/sift_match_2.png

216 KB
Loading

sift/bin/sift_match_c++.png

1.32 MB
Loading

sift/bin/surf

133 KB
Binary file not shown.

0 commit comments

Comments
 (0)