如果在python中图像角度不固定,如何旋转180度的图像?

时间:2017-07-11 13:24:54

标签: python opencv

Shape Detection screen

从给定的图像中我想将平坦的表面带到180度,即底部。以下代码我试过..

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)

以下是我输出的代码

Output_Image

从这段代码中我得到50%的正确输出,但表面不是180度。因为我不能保持图像角度静止,因为图像角度会变化,但我必须将平坦的表面带到底部。

0 个答案:

没有答案