Ask Your Question

Davide_sd's profile - activity

2018-10-26 10:29:20 -0500 received badge  Notable Question (source)
2018-03-22 06:27:36 -0500 received badge  Popular Question (source)
2017-08-25 14:00:26 -0500 received badge  Nice Question (source)
2016-10-04 06:46:45 -0500 received badge  Nice Answer (source)
2016-09-30 09:46:58 -0500 commented answer Detection of contours intersections

is it possible to mark two answers as both correct? truth is @kbarni answer nicely integrates with my current workflow.

2016-09-30 09:39:12 -0500 received badge  Teacher (source)
2016-09-30 08:21:06 -0500 received badge  Self-Learner (source)
2016-09-30 08:05:36 -0500 answered a question Detection of contours intersections

This is the Python code for @pklab answer, in case someone is in a hurry...

import numpy as np
import cv2

def getJunctions(src):
    # the hit-and-miss kernels to locate 3-points junctions to be used in each directions
    # NOTE: float type is needed due to limitation/bug in warpAffine with signed char
    k1 = np.asarray([
        0,  1,  0,
        0,  1,  0,
        1,  0,  1], dtype=float).reshape((3, 3))
    k2 = np.asarray([
        1,  0,  0,
        0,  1,  0,
        1,  0,  1], dtype=float).reshape((3, 3))
    k3 = np.asarray([
        0, -1,  1,
        1,  1, -1,
        0,  1, 0], dtype=float).reshape((3, 3))

    # Some useful declarations
    tmp = np.zeros_like(src)
    ksize = k1.shape
    center = (ksize[1] / 2, ksize[0] / 2) # INVERTIRE 0 E 1??????
    # 90 degrees rotation matrix
    rotMat = cv2.getRotationMatrix2D(center, 90, 1)
    # dst accumulates all matches
    dst = np.zeros(src.shape, dtype=np.uint8)

    # Do hit & miss for all possible directions (0,90,180,270)
    for i in range(4):
        tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k1.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)     
        dst = cv2.add(dst, tmp)
        tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k2.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)
        dst = cv2.add(dst, tmp)
        tmp = cv2.morphologyEx(src, cv2.MORPH_HITMISS, k3.astype(np.int8), tmp, (-1, -1), 1, cv2.BORDER_CONSTANT, 0)
        dst = cv2.add(dst, tmp)
        # Rotate the kernels (90deg)
        k1 = cv2.warpAffine(k1, rotMat, ksize)
        k2 = cv2.warpAffine(k2, rotMat, ksize)
        k3 = cv2.warpAffine(k3, rotMat, ksize)

    return dst






# This is your sample image (objects are black)
src = np.asarray([0, 1, 1, 1, 1, 1, 0, 0,
        1, 0, 1, 1, 1, 0, 1, 1,
        1, 1, 0, 0, 0, 1, 1, 1,
        1, 1, 1, 0, 0, 0, 1, 1,
        1, 0, 0, 1, 1, 1, 0, 1,
        0, 1, 1, 1, 1, 1, 0, 0,
        0, 1, 1, 1, 1, 1, 1, 1], dtype=np.uint8).reshape((7,8))

src *= 255;
# Morphology logic is: white objects on black foreground
src = 255 - src;

# Get junctions
junctionsScore = getJunctions(src)

# Draw markers where junction score is non zero
dst = cv2.cvtColor(src, cv2.COLOR_GRAY2RGB)
# find the list of location of non-zero pixels
junctionsPoint = cv2.findNonZero(junctionsScore)

for pt in junctionsPoint:
    pt = pt[0]
    dst[pt[1], pt[0], :] = [0, 0, junctionsScore[pt[1], pt[0]]]

# show the result
winDst = "Dst"
winSrc = "Src"
winJunc = "Junctions"
cv2.namedWindow(winSrc, cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO);
cv2.namedWindow(winJunc, cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO);
cv2.namedWindow(winDst, cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO);
scale = 24
cv2.resizeWindow(winSrc, scale*src.shape[1], scale*src.shape[0])
cv2.resizeWindow(winJunc, scale*src.shape[1], scale*src.shape[0])
cv2.resizeWindow(winDst, scale*src.shape[1], scale*src.shape[0])
cv2.imshow(winSrc, src)
cv2.imshow(winJunc, junctionsScore)
cv2.imshow(winDst, dst)
cv2.waitKey()
2016-09-30 08:01:41 -0500 commented answer Detection of contours intersections

wow, hit and miss is really powerful! Thanks @pklab to show me this solution!!!

2016-09-29 19:28:24 -0500 received badge  Student (source)
2016-09-29 03:48:53 -0500 commented answer Detection of contours intersections

top notch! thank you for your help :D

2016-09-29 03:47:57 -0500 received badge  Supporter (source)
2016-09-29 03:47:54 -0500 received badge  Scholar (source)
2016-09-28 17:06:34 -0500 received badge  Editor (source)
2016-09-28 17:03:52 -0500 commented answer Detection of contours intersections

unfortunately the contours are not straitgh lines, they come from random shapes, so i can't use the line equations. :(

2016-09-28 07:22:21 -0500 asked a question Detection of contours intersections

I have two contour images and I would like to find the contours intersection. So far I've succesfully used the logical and operation, unfortunately I've fallen into this case:

image description

As you can see, the logical and operation is going to fail because there isn't any intersection. If the red contours is in image A, and blue contour is in image B, I would like a way to return the two red pixel indicated by the arrow. Does openCV provide some function to do that? In case there isn't, how would you solve this problem?

PS: the contours come from random shapes, they are NOT straight lines!

Thank you.