This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from snucvpip/jisang
Jisang
- Loading branch information
Showing
44 changed files
with
875 additions
and
101 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,90 @@ | ||
import numpy as np | ||
|
||
from pre_process.align import * | ||
import os | ||
import cv2 | ||
import math | ||
from skimage.feature import peak_local_max | ||
from PIL import Image | ||
|
||
snapshot_info = [] | ||
|
||
def GetLimitNxNy(img): | ||
pim = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
height, width = pim.shape | ||
n_pixel = 30 | ||
|
||
coordinates = np.array(peak_local_max(pim, min_distance=3)) | ||
N = coordinates.shape[0] | ||
|
||
d = np.ceil(np.sqrt(width*height/N)) | ||
|
||
return tuple(np.array([width, height]) // int(d * n_pixel)) | ||
|
||
|
||
def Snapshot(img, resultdir, level=3): | ||
#################### EXPLANATION ##################### | ||
# This function makes snapshots of each level | ||
# The level is equivalent of magnification (the higher level is, the more magnified image is) | ||
# The nx, ny determines dividing range | ||
# the cropped images in neighborhood have same area for post-processing | ||
###################################################### | ||
|
||
################### USAGE EXAMPLE #################### | ||
# Snapshot(5, 2, 2) | ||
# This usage will divide image in 5 levels. | ||
# The maximum number of dividing is 2 in x, y axis | ||
# nx = [1, 3, 5] | ||
# ny = [1, 3, 5] | ||
# level0 = original input image | ||
# level1 = 3 pieces in x-axis X 3 pieces in y-axis = 9 pieces | ||
# level2 = 5 pieces in x-axis X 5 pieces in y-axis = 25 pieces | ||
###################################################### | ||
global snapshot_info | ||
nx, ny = GetLimitNxNy(img) | ||
list_nx = np.ceil(np.linspace(1, nx, level)).astype(int) | ||
list_ny = np.ceil(np.linspace(1, ny, level)).astype(int) | ||
|
||
dir_path = resultdir | ||
|
||
if not os.path.exists(dir_path): | ||
os.makedirs(dir_path) | ||
|
||
snapshot_info = [] | ||
for i in range(level): | ||
|
||
width = int(math.ceil(img.shape[1] * 2 / (list_nx[i] + 1))) | ||
height = int(math.ceil(img.shape[0] * 2 / (list_ny[i] + 1))) | ||
|
||
num_image = 1 | ||
stride = (math.floor(width / 2), math.floor(height / 2)) | ||
y_start, y_end = (0, height) | ||
|
||
for y in range(list_ny[i]): | ||
x_start, x_end = (0, width) | ||
|
||
for x in range(list_nx[i]): | ||
cv2.imwrite(dir_path + '/' + str(i) + '-' + str(num_image) + '.png', img[y_start:y_end, x_start:x_end]) | ||
if list_nx[i] >= 2 and x == list_nx[i] - 2: | ||
x_start = img.shape[1] - 1 - width | ||
x_end = img.shape[1] - 1 | ||
else: | ||
x_start += stride[0] | ||
x_end += stride[0] | ||
|
||
num_image += 1 | ||
|
||
if list_ny[i] >= 2 and y == list_ny[i] - 2: | ||
y_start = img.shape[0] - 1 - height | ||
y_end = img.shape[0] - 1 | ||
else: | ||
y_start += stride[1] | ||
y_end += stride[1] | ||
|
||
snapshot_info.append(((list_nx[i], list_ny[i]), (width, height), stride)) | ||
# print('level{} Complete, nx: {}, ny: {}, width: {}, height: {}'.format(i, list_nx[i], list_ny[i], width, height)) | ||
|
||
|
||
def main(source, target, resultdir): | ||
img = cv2.imread(source) | ||
Snapshot(img, resultdir) |
Large diffs are not rendered by default.
Oops, something went wrong.
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.
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
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
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 |
---|---|---|
@@ -1,29 +1,57 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import os | ||
import cv2 as cv | ||
import cv2 | ||
from skimage.feature import peak_local_max | ||
from sklearn.cluster import KMeans | ||
|
||
def dominantColors(img, n_clusters=3): | ||
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
|
||
#reshaping to a list of pixels | ||
img = img.reshape((img.shape[0] * img.shape[1], 3)) | ||
|
||
#using k-means to cluster pixels | ||
kmeans = KMeans(n_clusters, max_iter=1) | ||
kmeans.fit(img) | ||
|
||
#the cluster centers are our dominant colors. | ||
return kmeans.cluster_centers_ | ||
|
||
def smoothing(img): | ||
pim = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
height, width = pim.shape | ||
|
||
coordinates = np.array(peak_local_max(pim, min_distance=3)) | ||
N = coordinates.shape[0] | ||
|
||
d = np.sqrt(width*height/N) | ||
z = int(d/2) | ||
|
||
im2 = img.copy().astype(np.uint32) | ||
for y, x in coordinates: | ||
# if 100 < pim[y, x] < 150: continue | ||
im2[y-z:y+z, x-z:x+z] = (im2[y-z:y+z, x-z:x+z] + img[y, x]) / 2 | ||
|
||
return im2.astype(np.uint8) | ||
|
||
def seg(img): | ||
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) | ||
G = cv.GaussianBlur(gray,(3,3),0.5) | ||
rgb = cv.cvtColor(img,cv.COLOR_BGR2RGB) | ||
ret, thresh = cv.threshold(G,0,255, cv.THRESH_BINARY_INV+cv.THRESH_OTSU) | ||
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | ||
G = cv2.GaussianBlur(gray,(3,3),0.5) | ||
rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) | ||
ret, thresh = cv2.threshold(G,0,255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) | ||
|
||
I = np.array(rgb) | ||
x, y, z = I.shape | ||
I[thresh <= 254] = [255,255,255] | ||
# for i in range(x): | ||
# for j in range(y): | ||
# if thresh[i][j] <=254: | ||
# I[i][j] = [255,255,255] | ||
|
||
I2 = cv.GaussianBlur(I, (3,3), 0.5) | ||
I[thresh <= 254] = [255,255,255] | ||
I2 = cv2.GaussianBlur(I, (3,3), 0.5) | ||
return I2 | ||
|
||
def main(target, resultdir): | ||
img = cv.imread(target) | ||
I = seg(img) | ||
img = cv2.imread(target) | ||
img = smoothing(img) | ||
I_seg = seg(img) | ||
assert os.path.exists(resultdir), print('result directory not exists') | ||
filename = os.path.basename(target) | ||
plt.imsave(os.path.join(resultdir,filename), I) | ||
plt.imsave(os.path.join(resultdir, filename), I_seg) | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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.
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,31 @@ | ||
from skimage.metrics import mean_squared_error, peak_signal_noise_ratio, structural_similarity | ||
import os | ||
import cv2 | ||
from matplotlib.pyplot import imshow | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
datadir = './data' | ||
src_name = 'background.png' | ||
tar_name = 'moire.png' | ||
|
||
# grayscale | ||
src = cv2.imread(os.path.join(datadir, src_name), cv2.IMREAD_GRAYSCALE) | ||
tar = cv2.imread(os.path.join(datadir, tar_name), cv2.IMREAD_GRAYSCALE) | ||
|
||
mse = mean_squared_error(src, tar) | ||
psnr = peak_signal_noise_ratio(src, tar) | ||
ssim, diff = structural_similarity(src, tar, multichannel=True, full=True) | ||
|
||
diff = (diff * 255).astype("uint8") | ||
|
||
fig, ax = plt.subplots(ncols=4, figsize=(15, 5)) | ||
ax[0].imshow(src, 'gray') | ||
ax[0].set_title('Source') | ||
ax[1].imshow(tar, 'gray') | ||
ax[1].set_title('Target') | ||
ax[2].imshow(np.abs(src-tar), 'gray') | ||
ax[2].set_title(f'MSE:{round(mse,2)}, PSNR:{round(psnr,2)}, SSIM:{round(ssim,2)}') | ||
ax[3].imshow(diff, cmap='gray') | ||
ax[3].set_title('Difference') | ||
plt.savefig(os.path.join(datadir, 'result_gray.png')) |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.