I’m writing a program to collect a series of images from a USB microscope and generate an average as a way of denoising. I’m having to convert the image into a numpy array, sum the pixels then divide before converting back to an image.
Somewhere along the line the channels get mixed so my output is BGR not RGB, I’m trying to use cvtcolor to rectify that but get the error:
—> 55 RgbImage = cv2.cvtcolor(ave_image,cv2.COLOR_BGR2RGB) 56 return RgbImage
AttributeError: ‘module’ object has no attribute ‘cvtcolor’
Presumably cvtcolour isn’t being imported from CV2 but I can’t see why. Here’s the relevant code sections.
Lastly, although the averaging works fine, is there a more elegant way to do it? I need a simple mean not a weighted average.
import os, numpy, PIL from PIL import Image import cv2 import time frameaverage=5 def average_image(): arr=numpy.zeros((h,w,3),numpy.float) # Build up average pixel intensities as floating for im in xrange(frameaverage): camera_capture = get_image() imarr=numpy.array(camera_capture,dtype=numpy.float) arr=arr+imarr/frameaverage # Round values convert to 8-bit integer arr=numpy.array(numpy.round(arr),dtype=numpy.uint8) ave_image=Image.fromarray(arr,mode="RGB") RgbImage = cv2.cvtcolor(ave_image,cv2.COLOR_BGR2RGB) return RgbImage
Advertisement
Answer
In cv2 function cvtcolor renamed to cvtColor
. So you should use RgbImage = cv2.cvtColor(ave_image,cv2.COLOR_BGR2RGB)