Skip to content

Commit

Permalink
fi bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liupeng89 committed May 30, 2018
1 parent bb8cef4 commit b15961e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 37 deletions.
Binary file added 0034串.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 13 additions & 15 deletions autoStrokeExtracting.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
getCornerPointsOfImage, getClusterOfCornerPoints, getCropLinesPoints, \
getConnectedComponentsOfGrayScale, getAllMiniBoundingBoxesOfImage, getCornersPoints, \
getContourImage, getValidCornersPoints, getDistanceBetweenPointAndComponent, \
isIndependentCropLines, mergeBkAndComponent
isIndependentCropLines, mergeBkAndComponent, isValidComponent


def autoStrokeExtracting(index, image, threshold_value=200):
Expand Down Expand Up @@ -56,7 +56,7 @@ def autoStrokeExtracting(index, image, threshold_value=200):
return strokes

# corner area points
corners_all_points = getCornersPoints(image.copy(), contour_img)
corners_all_points = getCornersPoints(image.copy(), contour_img, blockSize=3, ksize=3, k=0.04)
corners_points = getValidCornersPoints(corners_all_points, cross_points, end_points, distance_threshold=30)
print("corners points num: %d" % len(corners_points))

Expand All @@ -75,6 +75,7 @@ def autoStrokeExtracting(index, image, threshold_value=200):
# cluster corners points based on the cross point
corner_points_cluster = getClusterOfCornerPoints(corners_points, cross_points)

# cropping lines based on the corner points
crop_lines = getCropLines(corner_points_cluster)

for line in crop_lines:
Expand All @@ -90,7 +91,7 @@ def autoStrokeExtracting(index, image, threshold_value=200):
for x in range(contour_gray.shape[1]):
if labels[y][x] == r:
img_[y][x] = 0.0
if img_[0][0] != 0.0:
if img_[0][0] != 0.0 and isValidComponent(img_, img_bit):
components.append(img_)

print("components num : %d" % len(components))
Expand All @@ -115,9 +116,9 @@ def autoStrokeExtracting(index, image, threshold_value=200):
line = crop_lines[i]
for j in range(len(components)):
dist_startpt = getDistanceBetweenPointAndComponent(line[0], components[j])
print("dist startpt:%f" % dist_startpt)
# print("dist startpt:%f" % dist_startpt)
dist_endpt = getDistanceBetweenPointAndComponent(line[1], components[j])
print("dist end pt: %f" % dist_endpt)
# print("dist end pt: %f" % dist_endpt)

if dist_startpt < 3 and dist_endpt < 3:
cv2.line(components[j], line[0], line[1], 0, 1)
Expand Down Expand Up @@ -226,24 +227,21 @@ def autoStrokeExtracting(index, image, threshold_value=200):
# add to strokes
strokes.append(bk)

cv2.imshow("radical_%d" % index, contour_rgb)
cv2.imshow("radical_gray_%d" % index, contour_gray)
# check the stroke is valid
for i in range(len(strokes)):
stroke = strokes[i]

# for i in range(len(overlap_components)):
# cv2.imshow("over_%d_com_%d" % (index, i), overlap_components[i])

# for i in range(len(components)):
# cv2.imshow("ra_%d_com_%d" % (index, i), components[i])

# for i in range(len(strokes)):
# cv2.imshow("ra_%d_stroke_%d" % (index, i), strokes[i])
cv2.imshow("radical_%d" % index, contour_rgb)
cv2.imshow("radical_gray_%d" % index, contour_gray)

return strokes


def main():
# 1133壬 2252支 0631叟 0633口 0242俄 0195佛 0860善 0059乘 0098亩
path = "2252支.jpg"
# 1133壬 2252支 0631叟 0633口 0242俄 0195佛 0860善 0059乘 0098亩 0034串
path = "0034串.jpg"

img_rgb = cv2.imread(path)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
Expand Down
4 changes: 2 additions & 2 deletions charactersegmentationmainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def setupUi(self, MainWindow):
self.image_gview.setObjectName("image_gview")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(20, 330, 201, 291))
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(20, 350, 201, 271))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
Expand All @@ -108,7 +108,7 @@ def setupUi(self, MainWindow):

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
MainWindow.setWindowTitle(_translate("MainWindow", "Chinese Calligraphy Segmentation Tool"))
self.open_btn.setText(_translate("MainWindow", "Open"))
self.grayscale_btn.setText(_translate("MainWindow", "Grayscale"))
self.convert_btn.setText(_translate("MainWindow", "Convert"))
Expand Down
6 changes: 3 additions & 3 deletions charactersegmentationmainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>Chinese Calligraphy Segmentation Tool</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="verticalLayoutWidget">
Expand Down Expand Up @@ -163,9 +163,9 @@ Distance:</string>
<property name="geometry">
<rect>
<x>20</x>
<y>330</y>
<y>350</y>
<width>201</width>
<height>291</height>
<height>271</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
Expand Down
25 changes: 23 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,28 @@
import cv2
import numpy as np

path = "2252支.jpg"
from utils.Functions import getCornersPoints, getContourImage, getSkeletonOfImage, removeExtraBranchesOfSkeleton, \
getEndPointsOfSkeletonLine, getCrossPointsOfSkeletonLine, getValidCornersPoints

path = "0034串.jpg"


img = cv2.imread(path, 0)
img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)

contour_img = getContourImage(img)

corner_img = np.float32(img)
dst = cv2.cornerHarris(corner_img, 2, 3, 0.04)
dst = cv2.cornerHarris(corner_img, 3, 3, 0.04)
dst = cv2.dilate(dst, None)

_, img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)

skeleton_img = getSkeletonOfImage(img)
# skeleton_img = removeExtraBranchesOfSkeleton(skeleton_img)
end_points = getEndPointsOfSkeletonLine(skeleton_img) # end points
cross_points = getCrossPointsOfSkeletonLine(skeleton_img) # crois

corners_area_points = []
for y in range(dst.shape[0]):
for x in range(dst.shape[1]):
Expand All @@ -137,7 +150,15 @@
for pt in corners_area_points:
img_rgb[pt[1]][pt[0]] = (0, 0, 255)

corner_points = getCornersPoints(img, contour_img, 3, 3, 0.04)
corner_points = getValidCornersPoints(corner_points, cross_points, end_points, distance_threshold=45)
print("corner point num: %d" % len(corner_points))

for pt in corner_points:
img_rgb[pt[1]][pt[0]] = (0, 255, 0)

cv2.imshow("rgb", img_rgb)
cv2.imshow("skeleton", skeleton_img)

cv2.waitKey(0)
cv2.destroyAllWindows()
53 changes: 38 additions & 15 deletions utils/Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,10 +1759,8 @@ def getCropLines(corner_points_cluster):
crop_lines.append((corner_clt))
elif len(corner_clt) == 1:
print("One corner point")


elif len(corner_clt) == 4:

print(corner_clt)
# based on the y list to detect rectangle or diamond
y_list = [corner_clt[0][1], corner_clt[1][1], corner_clt[2][1], corner_clt[3][1]]
y_list = sorted(y_list)
Expand Down Expand Up @@ -1805,15 +1803,17 @@ def getCropLines(corner_points_cluster):
P4
"""
P1 = P2 = P3 = P4 = None
used_point_index = []
for i in range(len(corner_clt)):
if corner_clt[i][1] == y_list[0]:
P1 = corner_clt[i]
if corner_clt[i][1] == y_list[1]:
P2 = corner_clt[i]
if corner_clt[i][1] == y_list[2]:
P4= corner_clt[i]
if corner_clt[i][1] == y_list[3]:
P3 = corner_clt[i]
if corner_clt[i][1] == y_list[0] and 0 not in used_point_index:
P1 = corner_clt[i]; used_point_index.append(0)
elif corner_clt[i][1] == y_list[1] and 1 not in used_point_index:
P2 = corner_clt[i]; used_point_index.append(1)
elif corner_clt[i][1] == y_list[2] and 2 not in used_point_index:
P4= corner_clt[i]; used_point_index.append(2)
elif corner_clt[i][1] == y_list[3] and 3 not in used_point_index:
P3 = corner_clt[i]; used_point_index.append(3)

if P4[0] >= P2[0]:
# change order of P2 / P4
curr_pt = P2; P2 = P4; P4 = curr_pt
Expand Down Expand Up @@ -1918,7 +1918,7 @@ def getClusterOfCornerPoints(corner_points, cross_points, threshold_distance=30)
return corner_points_cluster


def getCornersPoints(grayscale, contour_img):
def getCornersPoints(grayscale, contour_img, blockSize=3, ksize=3, k=0.04):
"""
Get corners points
:param grayscale:
Expand All @@ -1932,7 +1932,7 @@ def getCornersPoints(grayscale, contour_img):

# corner area points
corner_img = np.float32(grayscale)
dst = cv2.cornerHarris(corner_img, 2, 3, 0.04)
dst = cv2.cornerHarris(corner_img, blockSize, ksize, k)
dst = cv2.dilate(dst, None)

corners_area_points = []
Expand Down Expand Up @@ -2002,7 +2002,8 @@ def getValidCornersPoints(corners_all_points, cross_points, end_points, distance
for pt in corners_all_points:
dist_cross = min_distance_point2pointlist(pt, cross_points)
dist_end = min_distance_point2pointlist(pt, end_points)
if dist_cross < distance_threshold and dist_end > distance_threshold / 3.:
if dist_cross < distance_threshold and dist_end > distance_threshold / 4.:
# if dist_cross < distance_threshold:
corners_points.append(pt)

return corners_points
Expand Down Expand Up @@ -2070,4 +2071,26 @@ def mergeBkAndComponent(bk, component):
if component[y][x] == 0.0:
bk[y][x] = 0.0

return bk
return bk


def isValidComponent(component, grayscale):
"""
Check the component is valid or not.
:param component:
:param grayscale:
:return:
"""
num_valid_pixels = 0
num_pixels_of_component = 0
for y in range(component.shape[0]):
for x in range(component.shape[1]):
if component[y][x] == 0.0:
num_pixels_of_component += 1
if component[y][x] == grayscale[y][x]:
num_valid_pixels += 1

if num_valid_pixels >= 0.8 * num_pixels_of_component:
return True
else:
return False

0 comments on commit b15961e

Please sign in to comment.