2

Shape Detection screen

From given image I want to bring flat surface to 180 degree i.e at the bottom. Following code i have tried..

def get_line(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150, apertureSize=3)
    lines = cv2.HoughLines(edges, 1, np.pi / 180, 50)
    for rho, theta in lines[0]:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * rho
        y0 = b * rho
        x1 = int(x0 + 1000 * (-b))
        y1 = int(y0 + 1000 * (a))
        x2 = int(x0 - 1000 * (-b))
        y2 = int(y0 - 1000 * (a))

        # cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
    # print(x1, x2, y1, y2)
    cx = int((x1 + x2) / 2)
    cy = int((y1 + y2) / 2)
    # print("abc",cx, cy)
    deg = math.degrees(math.atan2(cx, cy))
    print(deg)
    # cv2.circle(img,(cx,cy),5,(0,255,0),-1)

    cv2.imshow('frame', img)
    cv2.waitKey(0)
    return cx, cy

def rotation():

        img=cv2.imread("pr43.jpg")
        cx,cy=get_line(img)

        height, width, _ = img.shape
        # print(width, height)
        xpercent = int(abs((cx / width) * 100))
        ypercent = int(abs((cy / height) * 100))

        # print ("XPercent",xpercent)
        # print("YPercent",ypercent)
        # print("xpercent, ypercent", xpercent, ypercent)
        if xpercent > 50:
            print("point in right palne and vertical")
            # Todo: rotate clock by 90*
            r = imutils.rotate_bound(img, 90)
            cv2.imshow("roatated", r)
            cv2.waitKey(0)

        elif xpercent > 0 and 0 < ypercent < 50:
            print("point in upper left plane and vertical")
            # Todo: rotate anti-clock by 90*
            r = imutils.rotate_bound(img, -90)
            cv2.imshow("roatated", r)
            cv2.waitKey(0)
        elif xpercent <= 0:

            print("point in upper left plane and Horizontal")
            # Todo: rotate clock by 180*
            r = imutils.rotate_bound(img, 180)
            cv2.imshow("roatated", r)
        elif xpercent < 50 and ypercent > 50:
            print("No rotation needed")
            r = img

       cv2.imwrite("Output_Image.jpg", r)

Below is my output of the code

Output_Image

From this code I'am getting 50% correct output but the surface is not coming at 180 degree. As i cant keep the image angle static because image angle will vary but i have to bring flat surface to Bottom.

Gaurav pal
  • 21
  • 3
  • [this thread](https://stackoverflow.com/a/32228759/1714410) highlights possible algorithmic steps you might want to try. The implementation there is in [tag:matlab], but I'm sure you can find opencv equivalent to most steps there as they are quite basic. – Shai Jul 11 '17 at 13:30
  • If you have the angle already (as far as I understood your code/post) then you can create a homography matrix and warp the image to rotate it. – api55 Jul 11 '17 at 13:56
  • If you need to rotate image using opencv, you might find [this answer](https://stackoverflow.com/a/14874111/1714410) relevant as well. – Shai Jul 11 '17 at 14:03

0 Answers0