裁剪图像四

时间:2019-06-06 18:08:30

标签: python opencv

裁剪后的图像显示为cv2.imshow,但不能使用cv2.imwrite保存它们。

我尝试了输入变量和类型为输出变量的问题,它们都给出了numpy.ndarray

import numpy as np
import cv2

def recotar_imagen(gray):
    #Read the image
    gray = cv2.imread('imagengris.jpg')
    print type (gray)

    #Means of the image within a matrix
    heigth, width = gray.shape[:2]
    n = width
    h = heigth
    x = 0
    y = 0

    #Formulas for half of the image in x y
    x2 = int(round ((n-1)/2)) 
    y2 = int(round ((h-1)/2))
    x3 =  x2 + 1
    y3 =  y2 + 1

    #Operator for the first image 
    #cropped_lu is for the image on the left above
    cropped_lu = gray[x:x2, y:y2]
    print type (cropped_lu)
    cv2.imshow("cropped_lu.jpg", cropped_lu)
    cv2.imwriter("cropped_lu.jp", cropped_lu)

    #Operator for the second image
    #cropped_ru is for the image on the right above
    cropped_ru = gray[x3:n-1, y:y2]
    print type (cropped_ru)
    cv2.imshow("cropped_ru.jpg", cropped_ru)
    cv2.write("cropped_ru.jpg", cropped_ru)

    #Operator for the third image
    #cropped_ld is for the image on the left below
    cropped_ld = gray[x:x2, y3:h-1]
    print type (cropped_ld)
    cv2.imshow("cropped_ld.jpg", cropped_ld)
    cv2.imsave("cropped_ld.jpg", cropped_ld)


    #Operator for the fourth image 
    #cropped_ld is for the image on the right below
    cropped_rd = gray[x2:n-1, y3:h-1]
    print type (cropped_rd)
    cv2.imshow("cropped_rd.jpg", cropped_rd)
    cv2.imwrite("cropped_rd.jpg", cropped_rd)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

错误日志

  

第55行,在recotar_imagen中   cv2.imwriter(“ cropped_lu.jp”,cropped_lu)

     

AttributeError:“模块”对象没有属性“ imwriter”

3 个答案:

答案 0 :(得分:-1)

方法是cv2.imwrite()而不是cv2.imwriter()

答案 1 :(得分:-1)

您想写imwrite而不是imwriter

答案 2 :(得分:-2)

修复了您的代码。问题:

  1. 您致电了cv2.imwriter(),该电话不存在。已将其更正为cv2.imwrite()
  2. 您试图用cv2.imwriter("cropped_lu.jp", cropped_lu)扩展名'.jp'保存图像,这不是图像的正确扩展名。更正为“ .jpg”
  3. 您致电了cv2.write(),该电话不存在。已将其更正为cv2.imwrite()
  4. 您呼叫了不存在的cv2.imsave()。已将其更正为cv2.imwrite()

    def recotar_imagen(gray):
            #Read the image
            gray = cv2.imread('imagengris.jpg')
            print(type (gray))
    
            #Means of the image within a matrix
            heigth, width = gray.shape[:2]
            n = width
            h = heigth
            x = 0
            y = 0
    
            #Formulas for half of the image in x y
            x2 = int(round ((n-1)/2)) 
            y2 = int(round ((h-1)/2))
            x3 =  x2 + 1
            y3 =  y2 + 1
    
            #Operator for the first image 
            #cropped_lu is for the image on the left above
            cropped_lu = gray[x:x2, y:y2]
            print( type (cropped_lu))
            cv2.imshow("cropped_lu.jpg", cropped_lu)
            cv2.imwrite("cropped_lu.jpg", cropped_lu)
    
            #Operator for the second image
            #cropped_ru is for the image on the right above
            cropped_ru = gray[x3:n-1, y:y2]
            print( type (cropped_ru))
            cv2.imshow("cropped_ru.jpg", cropped_ru)
            cv2.imwrite("cropped_ru.jpg", cropped_ru)
    
            #Operator for the third image
            #cropped_ld is for the image on the left below
            cropped_ld = gray[x:x2, y3:h-1]
            print( type (cropped_ld))
            cv2.imshow("cropped_ld.jpg", cropped_ld)
            cv2.imwrite("cropped_ld.jpg", cropped_ld)
    
    
            #Operator for the fourth image 
            #cropped_ld is for the image on the right below
            cropped_rd = gray[x2:n-1, y3:h-1]
            print( type (cropped_rd))
            cv2.imshow("cropped_rd.jpg", cropped_rd)
            cv2.imwrite("cropped_rd.jpg", cropped_rd)
    
            cv2.waitKey(0)
            cv2.destroyAllWindows()
    
    
    recotar_imagen("test")