This repository was archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimagecleaning.py
More file actions
59 lines (55 loc) · 1.48 KB
/
imagecleaning.py
File metadata and controls
59 lines (55 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import cv2 as cv
import numpy as np
import pdb
import argparse
from pathlib import Path, PurePath
import os
#669*497
def main():
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to image")
args = vars(ap.parse_args())
global remove
remove = False
global counter
counter = 0
red = []
orgimg = cv.imread(args['image'])
img = cv.imread(args['image'], cv.IMREAD_GRAYSCALE)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if(img[i][j] > 0):
red.append([i, j])
print(len(red))
if(len(red) > 10000):
for i in red:
img[i[0]][i[1]] = 0;
remove = True
kernel = np.ones((3,5),np.uint8)
img = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)
for i in range(img.shape[0]):
find = False
for j in range(img.shape[1]):
if(img[i][j] > 0):
counter = counter + 1
print(counter)
if(img[i][j] > 0 and counter > 100):
save_name_org = PurePath("object", Path(args['image']).stem + f"_shape.jpg")
print(save_name_org)
cv.imwrite(str(save_name_org), img)
save_name_org = PurePath("original_good", Path(args['image']).stem + f"_shape.jpg")
print(save_name_org)
cv.imwrite(str(save_name_org), orgimg)
find = True
break
if(find == True):
break
print(find, counter)
if(find == False):
print("here")
save_name_org = PurePath("trash", Path(args['image']).stem + f".jpg")
cv.imwrite(str(save_name_org), orgimg)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
main()