예제 #1
0
def apply_harris_corners(image):
    """run the harris corner detection function and dilate to increase point
       size.

    :param img: the loaded image
    :type img: cv2 image
    :return: copy of the image with the corner detections
    :rtype: cv2 image
    """

    # copy the image
    img = np.copy(image)

    # convert to gray before corner detection
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # get the corners
    corners = cv2.cornerHarris(gray, 2, 3, 0.04)

    # result is dilated for marking the corners, not important
    corners = cv2.dilate(corners, None)

    # put the corners on the image
    img[corners > 0.01 * corners.max()] = [0, 0, 255]

    return img
예제 #2
0
def get_corners(img, param1=2, param2=3, param3=0.04):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, param1, param2, param3)
    ret, dst = cv2.threshold(dst, 0.1 * dst.max(), 255, 0)
    dst = np.uint8(dst)
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
    corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)

    return corners
예제 #3
0
def harris_corner_detect(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    gray = cv.GaussianBlur(gray, (9, 9), 25)  # sigma
    # cv.imshow('GS gray', gray)
    # dst = cv.cornerHarris(gray, 2, 3, 0.04)
    dst = cv.cornerHarris(gray, 9, 19, 0.06)
    # 膨胀腐蚀-开操作,去除小的干扰角点
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (9, 9))
    dst = cv.morphologyEx(dst, cv.MORPH_OPEN, kernel)
    # 角点绘制
    # image[dst > 0.01 * dst.max()] = [0, 0, 255]
    image[dst > 0.01 * dst.max()] = [0, 0, 255]
    cv.imshow('image for harris', image)
예제 #4
0
def simple_harris(in_img):
    out_img = in_img
    gray_img = cv2.cvtColor(in_img, cv2.COLOR_BGR2GRAY)  # 转化灰度图
    dst = cv2.cornerHarris(gray_img, 2, 3, 0.04)  # 角点检测
    dst = cv2.dilate(dst, None)  # 膨胀,提升后续图像角点标注的清晰准确度
    out_img[dst > 0.01 * dst.max()] = (0, 0, 255)  # 画图秒点操作(要求彩色图)
    # len_x, len_y = in_img.shape[:2] # 获取图像尺寸
    # len_x = round(len_x) # 四舍五入取整
    # len_y = round(len_y)
    # for y in range(len_y):
    #     for x in range(len_x):
    #         if dst[x][y]>0.01*dst.max():
    #             out_img[x][y] = 255 # 返回满足条件的dst索引值,根据索引值来设置点的颜色
    return out_img
예제 #5
0
def detect_corners(img):
    """
    Finds corners of the image with Corner Harris
    :param img: img in which to find corners
    :return: img with corners drawn
    """
    blur = cv2.medianBlur(img, 5)
    gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 3, 0.04)

    # result is dilated for marking the corners, not important
    dst = cv2.dilate(dst, None)

    # Threshold for an optimal value, it may vary depending on the image.
    img[dst > 0.01 * dst.max()] = [0, 0, 255]
예제 #6
0
    def cornerHarris(self):
        img = cv2.imread(os.path.join(root, '..', 'static', 'photos', session['org_img']))
        img = np.float32(img)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        gray = np.float32(gray)
        dst = cv2.cornerHarris(gray, 2, 5, 0.07)

        # result is dilated for marking the corners, not important
        dst = cv2.dilate(dst, None)

        # Threshold for an optimal value, it may vary depending on the image.
        img[dst > 0.01 * dst.max()] = [0, 0, 255]
        filename = str(randint(1000000000, 9999999999)) + session['org_img']
        cv2.imwrite(os.path.join(root, '..', 'static', 'photos', filename), img)
        session['corner_img'] = filename
예제 #7
0
 def corner_extraction(image):
     """
     角点检测,没用到
     :param image:
     :return:
     """
     if len(image.shape) == 3:
         gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     else:
         gray = image.copy()
     dst = cv2.cornerHarris(image, 2, 3, 0.04)
     print('before dilate size:', dst.shape)
     dst = cv2.dilate(dst, None)
     print('after dilate size:', dst.shape)
     image = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
     image[dst > 0.01 * dst.max()] = [0, 0, 255]
     return image, dst
예제 #8
0
def corner(image):

    img=image.copy()

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 3, 0.04)

    # result is dilated for marking the corners, not important
    dst = cv2.dilate(dst, None)

    # Threshold for an optimal value, it may vary depending on the image.
    img[dst > 0.01 * dst.max()] = [0, 0, 255]

    #cv2.imshow('dst', image)
    #cv2.imwrite("foo/image_1_c.jpg", img)
    #print(img.shape)
    return img
예제 #9
0
from cv2 import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np

filename = 'image/harris.jpeg'
img = cv.imread(filename)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = np.float32(gray)
# 输入图像必须是 float32 ,最后一个参数在 0.04 到 0.05 之间
dst = cv.cornerHarris(gray, 2, 3, 0.04)
#result is dilated for marking the corners, not important
dst = cv.dilate(dst, None)
# Threshold for an optimal value, it may vary depending on the image.
img[dst > 0.01 * dst.max()] = [0, 0, 255]
cv.imshow('dst', img)
if cv.waitKey(0) & 0xff == 27:
    cv.destroyAllWindows()
예제 #10
0
# 转化为灰度图
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 二值化处理
ret, binary = cv.threshold(gray, 90, 255, cv.THRESH_BINARY)
cv_show('img', binary)
# 通过腐蚀去除白色噪声
kernel = np.ones((5, 5), np.uint8)
binary = cv.erode(binary, kernel)
# 平滑处理
binary = cv.medianBlur(binary, 5, 0)
cv_show('img', binary)

# Harris检测之前转换成float32类型
gray = np.float32(binary)
# 这里中间两个参数很重要,一开始没注意到
dst = cv.cornerHarris(gray, 6, 9, 0.04)
dst = cv.dilate(dst, None)
img[dst > 0.0281 * dst.max()] = [0, 0, 255]

# 以二值图形式显示修改的地方
ret, dst = cv.threshold(dst, 0.01 * dst.max(), 255, 0)
cv_show('img', dst)
# 保存图片
cv.imwrite('Tests/28-31_2.jpg', img)

# dst = np.uint8(dst)
# ret, labels, stats, centroids = cv.connectedComponentsWithStats(dst)

# criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.001)

# corners = cv.cornerSubPix(gray, np.float32(
예제 #11
0
import matplotlib.pyplot as plt
import numpy as np
from cv2 import cv2

well = plt.imread('K-016.jpg')
well = cv2.cvtColor(well, cv2.COLOR_BGRA2GRAY)
plt.subplot(151)
plt.title('A')
plt.imshow(well)

harris = cv2.cornerHarris(well, 4, 1, 0.00)
plt.subplot(152)
plt.title('B')
plt.imshow(harris)

x, thr = cv2.threshold(harris, 0.1 * harris.max(), 255, cv2.THRESH_BINARY)
thr = thr.astype('uint8')
plt.subplot(153)
plt.title('C')
plt.imshow(thr)

dst, contours, hierarchy = cv2.findContours(thr.copy(), cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_NONE)
areas = map(lambda x: cv2.contourArea(cv2.convexHull(x)), contours)
max_i = areas.index(max(areas))
d = cv2.drawContours(np.zeros_like(thr), contours, max_i, 255, 1)
plt.subplot(154)
plt.title('D')
plt.imshow(d)

rect = cv2.minAreaRect(contours[max_i])
예제 #12
0
from cv2 import cv2
import numpy as np

file_path = 'src/house.jpg'

img = cv2.imread(file_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 해리스 코너 검출 ---①
corner = cv2.cornerHarris(gray, 2, 3, 0.04)
# 변화량 결과의 최대값 10% 이상의 좌표 구하기 ---②
coord = np.where(corner > 0.1 * corner.max())
coord = np.stack((coord[1], coord[0]), axis=-1)

# 코너 좌표에 동그리미 그리기 ---③
for x, y in coord:
    cv2.circle(img, (x, y), 5, (0, 0, 255), 1, cv2.LINE_AA)

# 변화량을 영상으로 표현하기 위해서 0~255로 정규화 ---④
corner_norm = cv2.normalize(corner, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# 화면에 출력
corner_norm = cv2.cvtColor(corner_norm, cv2.COLOR_GRAY2BGR)
merged = np.hstack((corner_norm, img))
cv2.imshow('Harris Corner', merged)
cv2.waitKey()
cv2.destroyAllWindows()
예제 #13
0
## 모서리 감지
# cornerHarris = 해리스 모서리 감지
# 두 개의 경계선이 교차하는 지점을 감지하는 방법
# 윈도(이웃, 패치)안 픽셀이 작은 움직임에도 크게 변하는 윈도 찾기
image_bgr = cv2.imread('../img/black.jpg')
image_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
image_gray = np.float32(image_gray)

block_size = 2  # 모서리 감지 매개변수를 설정
aperture = 29
free_parameter = 0.04

# block_size = 각 픽셀에서 모서리 감지에 사용되는 이웃 픽셀 크기
# aperture = 사용하는 소벨 커널 크기
detector_responses = cv2.cornerHarris(image_gray, block_size, aperture,
                                      free_parameter)  # 모서리를 감지
detector_responses = cv2.dilate(detector_responses, None)  # 모서리 표시를 부각시킵니다.

# 임계값보다 큰 감지 결과만 남기고 흰색으로 표시합니다.
threshold = 0.02
image_bgr[detector_responses > threshold * detector_responses.max()] = [
    255, 255, 255
]

image_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)  # 흑백으로 변환

plt.imshow(image_gray, cmap="gray"), plt.axis("off")  # 이미지 출력
plt.show()

# 가능성이 높은 모서리를 출력합니다.
plt.imshow(detector_responses, cmap='gray'), plt.axis("off")
예제 #14
0
# corners = cv2.goodFeaturesToTrack(gray,84,0.01,10)
# for i in corners:
#     x,y = i.ravel()
#     cv2.circle(pointImg,(x,y),3,255,-1)
# plt.imshow(pointImg),plt.show()

kernel = np.ones((5, 5), np.uint8)
cv2.imshow('gray2', gray)
gray = cv2.erode(gray, kernel, 2)  # 通过腐蚀运算填补黑块内部的白色空隙
cv2.imshow('gray1', gray)
gray = cv2.threshold(gray, 90, 255, cv2.THRESH_BINARY)[1]  # 设置过滤无关边界内容
gray = cv2.GaussianBlur(gray, (3, 3), 0)  # 使图像模糊,便于减少无关图像干扰

gray = np.float32(gray)

dst = cv2.cornerHarris(gray, 2, 3, 0.04)  # 找到Harris角点
dst = cv2.dilate(dst, None)  # 膨胀操作
ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)  # 二值化,除去不相关图像
dst = cv2.GaussianBlur(dst, (3, 3), 0)  # 使图像模糊,便于减少无关图像干扰
dst = np.uint8(dst)

centroids = cv2.connectedComponentsWithStats(dst)[3]  # 寻找连通区域
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001
            )  # 设置终止条件,迭代30次或移动0.001
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1),
                           criteria)  # cv2.cornerSubPix()检测十分精细,但不便于显示

res = np.hstack((centroids, corners))
res = np.int0(res)
# cv2.cornerHarris检测出的角点,粗略
pointImg[dst > 0.1012 * dst.max()] = [0, 0, 255]