简体   繁体   中英

Error: OpenCV, Unsupported depth of input image

I would like to create a function which will randomly change a brightness of picture.

I have this imports:

import math
import random
import numpy as np

import cv2
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib.colors import Colormap
from mpl_toolkits.axes_grid1 import ImageGrid

from io import BytesIO
from google.colab import files

So then I upload my image into my google colab workspace:

uploaded = files.upload()
im = Image.open(BytesIO(uploaded['aug_cat.jpg']))
plt.imshow(im)
plt.show()

So then I get this picture . Then I make this to convert my image into floats:

img = cv2.imread('aug_cat.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype(float) / 255

And finally I try to use this function:

def aug_bright(img, factor = random.uniform(-10, 10)):
  hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
  hsv = np.array(hsv, dtype=np.float64)
  hsv[:, :, 2] = hsv[:, :, 2] * (factor)
  hsv[:, :, 2][hsv[:, :, 2] > 255] = 255 
  img = cv2.cvtColor(cv2.UMat(im), cv2.COLOR_HSV2RGB)
  return img

I try to call this function and show the result:

bright_img = aug_bright(img)
plt.imshow(bright_img)
plt.show()

But I have this error:

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:94: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<3>; VDepth = cv::impl::{anonymous}::Set<0, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = (cv::impl::<unnamed>::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Unsupported depth of input image:
>     'VDepth::contains(depth)'
> where
>     'depth' is 6 (CV_64F)

How can I fix it? What is the problem?

Why don't you use ImageEnhance method ? Through factor you can change the brightness of the image. For example

from PIL import Image, ImageEnhance

#read the image
im = Image.open("sample-image.png")

#image brightness enhancer
enhancer = ImageEnhance.Brightness(im)

factor = 1 #gives original image
im_output = enhancer.enhance(factor)
im_output.save('original-image.png')

factor = 0.5 #darkens the image
im_output = enhancer.enhance(factor)
im_output.save('darkened-image.png')

factor = 1.5 #brightens the image
im_output = enhancer.enhance(factor)
im_output.save('brightened-image.png')

So my task was to write my own function ans that is why I did not use some support libraries.

I have solved my problem like this.

Firstly, my imports:

import math
import random
import numpy as np

import cv2
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib.colors import Colormap
from mpl_toolkits.axes_grid1 import ImageGrid

from io import BytesIO
from google.colab import files

Secondly, some google colab downloads and data preparing:

uploaded = files.upload()
im = Image.open(BytesIO(uploaded['aug_cat.jpg']))
img = cv2.imread('aug_cat.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype(np.float32, copy=False)
imgUMat = np.float32(img)

And thirdly, my custom function:

def aug_bright(imgUMat, factor = random.uniform(-10, 10)):
  """
  Random brightness change
  """
  hsv = cv2.cvtColor(imgUMat, cv2.COLOR_RGB2HSV)
  hsv = np.array(hsv, dtype=np.float32)
  hsv[:, :, 2] = hsv[:, :, 2] * (factor)
  hsv[:, :, 2][hsv[:, :, 2] > 255] = 255 
  imgUMat = cv2.cvtColor(cv2.UMat(imgUMat), cv2.COLOR_HSV2RGB)
  return img

And this is how I call it:

bright_img = aug_bright(imgUMat)
plt.imshow(bright_img)
plt.show()

I hope my answer will be useful for someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM