def _scaleTo8bit(self, img): ''' The pattern comparator need images to be 8 bit -> find the range of the signal and scale the image ''' r = scaleSignalCutParams(img, 0.02) # , nSigma=3) self.signal_ranges.append(r) return toUIntArray(img, dtype=np.uint8, range=r)
def export(img, path): if self.pResize.value(): img = cv2.resize(img, (self.pHeight.value(), self.pWidth.value()) ) else: img = img.copy() int_img = toUIntArray(img, cutNegative=self.pCutNegativeValues.value(), cutHigh=~self.pStretchValues.value()) cv2.imwrite(path,out(int_img)) print 'Saved image under %s' %path
def imwrite(path, img, dtype=None, cmap=None, **kwargs): ''' accepted file types are... for 8 and 16 bit grayscale and RGB images: Windows bitmaps - *.bmp, *.dib JPEG files - *.jpeg, *.jpg, *.jpe JPEG 2000 files - *.jp2 Portable Network Graphics - *.png WebP - *.webp Portable image format - *.pbm, *.pgm, *.ppm Sun rasters - *.sr, *.ras TIFF files - *.tiff, *.tif for binary (bool) masks: *.png for 32 bit float images: *.tiff dtype = (None, float,bool) all other dtypes are converted to either uint8 or uint16 cmap = display grayscale as rgb using colormap: 'flame', 'gnuplot2' etc. ''' if dtype in (float, np.float64, np.float32): assert path.endswith('.tiff') or path.endswith( '.tif'), 'float arrays can only be saved as TIFF/TIF images' from PIL import Image Image.fromarray(np.asfarray(img)).save(path) # ... PIL is about 3x faster than tifffile.imwrite elif dtype in (bool, np.bool): # this method at about 10x slower than cv2.imwrite # however, it generates 8x smaller images (1bit vs 8bit) assert path.endswith( '.png'), 'boolean masks can only be saved as PNG images' assert img.ndim == 2, 'can only save grayscale images as type(bool)' from png import Writer with open(path, 'wb') as f: s0, s1 = img.shape w = Writer(s1, s0, greyscale=True, bitdepth=1) if not img.dtype == np.uint16: img = img.astype(np.uint8) w.write(f, img) else: img = toUIntArray(img, dtype=dtype, **kwargs) if cmap is not None: assert img.ndim == 2, '<cmap> can only be used for grayscale images' img = applyColorMap(img, cmap) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) return cv2.imwrite(path, img)
def fn(path, img): r = self.pRange.value() if r == '0-max': r = (0, w.levelMax) elif r == 'min-max': r = (w.levelMin, w.levelMax) else: # 'current' r = w.ui.histogram.getLevels() int_img = toUIntArray(img, # cutNegative=self.pCutNegativeValues.value(), cutHigh=~self.pStretchValues.value(), range=r, dtype={'8 bit': np.uint8, '16 bit': np.uint16}[self.pDType.value()]) if isColor(int_img): int_img = cv2.cvtColor(int_img, cv2.COLOR_RGB2BGR) cv2.imwrite(path, transpose(int_img))
def fn(path, img): r = self.pRange.value() if r == '0-max': r = (0, w.levelMax) elif r == 'min-max': r = (w.levelMin, w.levelMax) else: #'current' r = w.ui.histogram.getLevels() int_img = toUIntArray( img, # cutNegative=self.pCutNegativeValues.value(), cutHigh=~self.pStretchValues.value(), range=r, dtype={ '8 bit': np.uint8, '16 bit': np.uint16 }[self.pDType.value()]) cv2.imwrite(path, out(int_img))
def _changeArrayDType(img, dtype, **kwargs): if dtype == 'noUint': return toNoUintArray(img) if issubclass(np.dtype(dtype).type, np.integer): return toUIntArray(img, dtype, **kwargs) return img.astype(dtype)
def imwrite(name, arr, **kwargs): return cv2.imwrite(name, toUIntArray(arr, **kwargs))
def _to8bitImg(img): if img.dtype == np.uint8: return img r = signalRange(img) return toUIntArray(img, dtype=np.uint8, range=r)
def imwrite(name, arr, dtype=None, **kwargs): if dtype in (float, np.float64, np.float32): # save as 32bit float tiff Image.fromarray(np.asfarray(arr)).save(name) else: return cv2.imwrite(name, toUIntArray(arr, dtype=dtype, **kwargs))
# dist = img2.copy()#pc.distort(img2, rotX=10, rotY=20) dist = pc.distort(img2, rotX=10, rotY=20) dist2 = dist.copy() pc.draw3dCoordAxis(dist2, thickness=2) pc.drawQuad(dist2, thickness=2) depth = pc.depthMap() # , distance=0) bg = dist[:, :, 0] < 10 depth[bg] = depth.mean() print('depth min:%s, max:%s' % (depth.min(), depth.max())) # 3a. CORRECT WITH QUAD: corr = pc.correct(dist) # 3b. CORRECT WITH WITHEN REFERENCE IMAGE pc.img = dist pc.setReference(img) corr2 = pc.correct(dist) ################## if 'no_window' not in sys.argv: cv2.imshow("1. original", img2) cv2.imshow("2. distorted", toUIntArray(dist2, dtype=np.uint8)) cv2.imshow("3a. corr. with quad", toUIntArray(corr, dtype=np.uint8)) cv2.imshow("depth", toUIntArray(depth, cutHigh=False, dtype=np.uint8)) cv2.imshow( "3b. corr. with ref. image", toUIntArray(corr2, dtype=np.uint8)) cv2.waitKey(0)
obj_corners = [(8, 2), (198, 6), (198, 410), (9, 411)] #LOAD CLASS: pc = PerspectiveCorrection(img.shape, do_correctIntensity=True, obj_height_mm=sy, obj_width_mm=sx) pc.setReference(obj_corners) img2 = img.copy() pc.drawQuad(img2, thickness=2) #2. DISTORT THE IMAGE: dist = pc.distort(img2, rotX=10, rotY=20) dist2 = dist.copy() pc.draw3dCoordAxis(dist2, thickness=2) pc.drawQuad(dist2, thickness=2) #3a. CORRECT WITH QUAD: corr = pc.correct(dist) #3b. CORRECT WITH WITHEN REFERENCE IMAGE # pc.img = dist pc.setReference(img) corr2 = pc.correct(dist) if 'no_window' not in sys.argv: cv2.imshow("1. original", img2) cv2.imshow("2. distorted", toUIntArray(dist2, dtype=np.uint8)) cv2.imshow("3a. corr. with quad", toUIntArray(corr, dtype=np.uint8)) cv2.imshow("3b. corr. with ref. image", toUIntArray(corr2, dtype=np.uint8)) cv2.waitKey(0)