Обрезать изображение в четыре - PullRequest
0 голосов
/ 06 июня 2019

Обрезанные изображения отображаются с 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: 'module'объект не имеет атрибута «imwriter»

Ответы [ 3 ]

0 голосов
/ 06 июня 2019

Вы хотели написать imwrite, а не imwriter.

0 голосов
/ 06 июня 2019

Исправил ваш код.Проблемы:

  1. Вы позвонили cv2.imwriter(), который не существует.Исправлено до cv2.imwrite()
  2. Вы пытались сохранить изображение с расширением .jp на cv2.imwriter("cropped_lu.jp", cropped_lu), которое не является правильным расширением для изображения.Исправил это в '.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")
    
0 голосов
/ 06 июня 2019

Метод cv2.imwrite(), а не cv2.imwriter()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...