-
Notifications
You must be signed in to change notification settings - Fork 0
/
normal.py
85 lines (59 loc) · 2.74 KB
/
normal.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Start pipeline
import os
from re import S
import sys
from tkinter import Frame
import cv2
import pytesseract
import numpy as np
def preprocessing():
#load images from folder
pathIn = "./Floor/Images/"
pathOut = "./FloorplanToBlender3d/Images/Normal/"
files = os.listdir(pathIn)
for file in files:
inputImage = cv2.imread(pathIn + file)
# Conversion to CMYK (just the K channel):
"""
# Convert to float and divide by 255:
imgFloat = inputImage.astype(np.float) / 255.
# Calculate channel K:
kChannel = 1 - np.max(imgFloat, axis=2)
# Convert back to uint 8:
kChannel = (255 * kChannel).astype(np.uint8)
t = [cv2.THRESH_BINARY, cv2.THRESH_BINARY_INV, cv2.THRESH_TRUNC, cv2.THRESH_TOZERO, cv2.THRESH_TOZERO_INV]
for x in t:
ret,thresh1 = cv2.threshold(kChannel,127,255,x)
cv2.imwrite(pathOut + file + str(x) + ".jpg", thresh1)
thres = cv2.adaptiveThreshold(inputImage,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
cv2.imwrite(pathOut + file + str(x) + ".jpg", thres)
# Use a little bit of morphology to clean the mask:
# Set kernel (structuring element) size:
kernelSize = 2
# Set morph operation iterations:
opIterations = 2
# Get the structuring element:
morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
# Perform closing:
binaryImage = cv2.morphologyEx(binaryImage, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101)
# Perform an area filter on the binary blobs:
minArea = 50
filteredImage = areaFilter(minArea, binaryImage)
# invert color image
filteredImage = cv2.bitwise_not(filteredImage)
"""
filteredImage = cv2.bitwise_not(inputImage)
#save image
cv2.imwrite(pathOut + file, inputImage)
def areaFilter(minArea, inputImage):
# Perform an area filter on the binary blobs:
componentsNumber, labeledImage, componentStats, componentCentroids = \
cv2.connectedComponentsWithStats(inputImage, connectivity=4)
# Get the indices/labels of the remaining components based on the area stat
# (skip the background component at index 0)
remainingComponentLabels = [i for i in range(1, componentsNumber) if componentStats[i][4] >= minArea]
# Filter the labeled pixels based on the remaining labels,
# assign pixel intensity to 255 (uint8) for the remaining pixels
filteredImage = np.where(np.isin(labeledImage, remainingComponentLabels) == True, 255, 0).astype('uint8')
return filteredImage
preprocessing()