def update_brightcont(self): # The algorithm is by Werner D. Streidt # (http://visca.com/ffactory/archives/5-99/msg00021.html) if self.contrast > 0: delta = 127. * self.contrast / 100 a = 255. / (255. - delta * 2) b = a * (self.brightness - delta) else: delta = -128. * self.contrast / 100 a = (256. - delta * 2) / 255. b = a * self.brightness + delta cv.ConvertScale(self.src_image, self.dst_image, a, b) cv.ShowImage("image", self.dst_image) cv.CalcArrHist([self.dst_image], self.hist) (min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist) cv.Scale(self.hist.bins, self.hist.bins, float(self.hist_image.height) / max_value, 0) cv.Set(self.hist_image, cv.ScalarAll(255)) bin_w = round(float(self.hist_image.width) / hist_size) for i in range(hist_size): cv.Rectangle(self.hist_image, (int(i * bin_w), self.hist_image.height), (int((i + 1) * bin_w), self.hist_image.height - cv.Round(self.hist.bins[i])), cv.ScalarAll(0), -1, 8, 0) cv.ShowImage("histogram", self.hist_image)
def gray_swap(image): new_image = cv.CreateImage((image.width, image.height), image.depth, image.nChannels) minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(image) scale = -1 shift = 255 cv.Scale(image, new_image, scale, shift) return new_image
def linear_translate(image): linear_image = cv.CreateImage((image.width, image.height), image.depth, image.nChannels) minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(image) scale = 255.0 / (maxVal - minVal) shift = -(scale * minVal) cv.Scale(image, linear_image, scale, shift) return linear_image
def scaleTo8U(bldIm, counts): w = bldIm.shape[1] h = bldIm.shape[0] mat = np.zeros((h, w, bldIm.shape[2]), dtype=np.int32) mat[:, :, 0] = np.where(counts == 0, 1, counts) mat[:, :, 1] = np.where(counts == 0, 1, counts) mat[:, :, 2] = np.where(counts == 0, 1, counts) mat = cv.fromarray(mat) cvbldIm = cv.fromarray(bldIm) bldIm8U = cv.CreateMat(h, w, cv.CV_8UC3) cv.Div(cvbldIm, mat, cvbldIm) cv.Scale(cvbldIm, bldIm8U, 1.0, 0) return np.asarray(bldIm8U)
if __name__ == "__main__": if len(sys.argv) > 1: im = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE) else: url = 'https://raw.github.com/opencv/opencv/master/samples/c/baboon.jpg' filedata = urllib2.urlopen(url).read() imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1) cv.SetData(imagefiledata, filedata, len(filedata)) im = cv.DecodeImageM(imagefiledata, cv.CV_LOAD_IMAGE_GRAYSCALE) realInput = cv.CreateImage( cv.GetSize(im), cv.IPL_DEPTH_64F, 1) imaginaryInput = cv.CreateImage( cv.GetSize(im), cv.IPL_DEPTH_64F, 1) complexInput = cv.CreateImage( cv.GetSize(im), cv.IPL_DEPTH_64F, 2) cv.Scale(im, realInput, 1.0, 0.0) cv.Zero(imaginaryInput) cv.Merge(realInput, imaginaryInput, None, None, complexInput) dft_M = cv.GetOptimalDFTSize( im.height - 1 ) dft_N = cv.GetOptimalDFTSize( im.width - 1 ) dft_A = cv.CreateMat( dft_M, dft_N, cv.CV_64FC2 ) image_Re = cv.CreateImage( (dft_N, dft_M), cv.IPL_DEPTH_64F, 1) image_Im = cv.CreateImage( (dft_N, dft_M), cv.IPL_DEPTH_64F, 1) # copy A to dft_A and pad dft_A with zeros tmp = cv.GetSubRect( dft_A, (0,0, im.width, im.height)) cv.Copy( complexInput, tmp, None ) if(dft_A.width > im.width): tmp = cv.GetSubRect( dft_A, (im.width,0, dft_N - im.width, im.height))