Python cv2.IMREAD_UNCHANGED Examples
The following are 30
code examples of cv2.IMREAD_UNCHANGED().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2
, or try the search function
.
Example #1
Source File: util.py From smashscan with MIT License | 8 votes |
def get_image_and_mask(img_location, gray_flag): # Load image from file with alpha channel (UNCHANGED flag). If an alpha # channel does not exist, just return the base image. img = cv2.imread(img_location, cv2.IMREAD_UNCHANGED) if img.shape[2] <= 3: return img, None # Create an alpha channel matrix with values between 0-255. Then # threshold the alpha channel to create a binary mask. channels = cv2.split(img) mask = np.array(channels[3]) _, mask = cv2.threshold(mask, 250, 255, cv2.THRESH_BINARY) # Convert image and mask to grayscale or BGR based on input flag. if gray_flag: img = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY) else: img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) return img, mask # Resize an image and mask based on an input scale ratio.
Example #2
Source File: image_helper.py From openseg.pytorch with MIT License | 7 votes |
def imfrombytes(content, flag='color'): """Read an image from bytes. Args: content (bytes): Image bytes got from files or other streams. flag (str): Same as :func:`imread`. Returns: ndarray: Loaded image array. """ imread_flags = { 'color': cv2.IMREAD_COLOR, 'grayscale': cv2.IMREAD_GRAYSCALE, 'unchanged': cv2.IMREAD_UNCHANGED } img_np = np.fromstring(content, np.uint8) flag = imread_flags[flag] if isinstance(flag, str) else flag img = cv2.imdecode(img_np, flag) return img
Example #3
Source File: utils_image.py From KAIR with MIT License | 7 votes |
def imread_uint(path, n_channels=3): # input: path # output: HxWx3(RGB or GGG), or HxWx1 (G) if n_channels == 1: img = cv2.imread(path, 0) # cv2.IMREAD_GRAYSCALE img = np.expand_dims(img, axis=2) # HxWx1 elif n_channels == 3: img = cv2.imread(path, cv2.IMREAD_UNCHANGED) # BGR or G if img.ndim == 2: img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) # GGG else: img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # RGB return img # -------------------------------------------- # matlab's imwrite # --------------------------------------------
Example #4
Source File: cnn_use.py From bonnet with GNU General Public License v3.0 | 6 votes |
def predict_code(img, net, FLAGS): # predict feature map from image # open image cvim = cv2.imread(img, cv2.IMREAD_UNCHANGED) if cvim is None: print("No image to open for ", img) return # predict mask from image start = time.time() code = net.predict_code(cvim, path=FLAGS.path + '/' + FLAGS.model, verbose=FLAGS.verbose) print("Prediction for img ", img, ". Elapsed: ", time.time() - start, "s") # reshape code to single dimension reshaped_code = np.reshape(code, (1, -1)) # print("Shape", reshaped_code.shape) # save code to text file filename = FLAGS.log + "/" + \ os.path.splitext(os.path.basename(img))[0] + ".txt" print("Saving feature map to: ", filename) np.savetxt(filename, reshaped_code, fmt="%.8f", delimiter=" ") return
Example #5
Source File: make_tinyimagenet_c.py From robustness with Apache License 2.0 | 6 votes |
def motion_blur(x, severity=1): c = [(10,1), (10,1.5), (10,2), (10,2.5), (12,3)][severity - 1] output = BytesIO() x.save(output, format='PNG') x = MotionImage(blob=output.getvalue()) x.motion_blur(radius=c[0], sigma=c[1], angle=np.random.uniform(-45, 45)) x = cv2.imdecode(np.fromstring(x.make_blob(), np.uint8), cv2.IMREAD_UNCHANGED) if x.shape != (64, 64): return np.clip(x[..., [2, 1, 0]], 0, 255) # BGR to RGB else: # greyscale to RGB return np.clip(np.array([x, x, x]).transpose((1, 2, 0)), 0, 255)
Example #6
Source File: util.py From BasicSR with Apache License 2.0 | 6 votes |
def read_img(env, path, size=None): '''read image by cv2 or from lmdb return: Numpy float32, HWC, BGR, [0,1]''' if env is None: # img img = cv2.imread(path, cv2.IMREAD_UNCHANGED) else: img = _read_img_lmdb(env, path, size) img = img.astype(np.float32) / 255. if img.ndim == 2: img = np.expand_dims(img, axis=2) # some images have 4 channels if img.shape[2] > 3: img = img[:, :, :3] return img #################### # image processing # process on numpy image ####################
Example #7
Source File: tools.py From keras-ocr with MIT License | 6 votes |
def read(filepath_or_buffer: typing.Union[str, io.BytesIO]): """Read a file into an image object Args: filepath_or_buffer: The path to the file, a URL, or any object with a `read` method (such as `io.BytesIO`) """ if isinstance(filepath_or_buffer, np.ndarray): return filepath_or_buffer if hasattr(filepath_or_buffer, 'read'): image = np.asarray(bytearray(filepath_or_buffer.read()), dtype=np.uint8) image = cv2.imdecode(image, cv2.IMREAD_UNCHANGED) elif isinstance(filepath_or_buffer, str): if validators.url(filepath_or_buffer): return read(urllib.request.urlopen(filepath_or_buffer)) assert os.path.isfile(filepath_or_buffer), \ 'Could not find image at path: ' + filepath_or_buffer image = cv2.imread(filepath_or_buffer) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
Example #8
Source File: make_imagenet_c.py From robustness with Apache License 2.0 | 6 votes |
def motion_blur(x, severity=1): c = [(10, 3), (15, 5), (15, 8), (15, 12), (20, 15)][severity - 1] output = BytesIO() x.save(output, format='PNG') x = MotionImage(blob=output.getvalue()) x.motion_blur(radius=c[0], sigma=c[1], angle=np.random.uniform(-45, 45)) x = cv2.imdecode(np.fromstring(x.make_blob(), np.uint8), cv2.IMREAD_UNCHANGED) if x.shape != (224, 224): return np.clip(x[..., [2, 1, 0]], 0, 255) # BGR to RGB else: # greyscale to RGB return np.clip(np.array([x, x, x]).transpose((1, 2, 0)), 0, 255)
Example #9
Source File: util.py From IKC with Apache License 2.0 | 6 votes |
def read_img(env, path, size=None): '''read image by cv2 or from lmdb return: Numpy float32, HWC, BGR, [0,1]''' if env is None: # img img = cv2.imread(path, cv2.IMREAD_UNCHANGED) else: img = _read_img_lmdb(env, path, size) img = img.astype(np.float32) / 255. if img.ndim == 2: img = np.expand_dims(img, axis=2) # some images have 4 channels if img.shape[2] > 3: img = img[:, :, :3] return img #################### # image processing # process on numpy image ####################
Example #10
Source File: corruptions.py From robustness with Apache License 2.0 | 6 votes |
def motion_blur(x, severity=1): c = [(10, 3), (15, 5), (15, 8), (15, 12), (20, 15)][severity - 1] output = BytesIO() x.save(output, format='PNG') x = MotionImage(blob=output.getvalue()) x.motion_blur(radius=c[0], sigma=c[1], angle=np.random.uniform(-45, 45)) x = cv2.imdecode(np.fromstring(x.make_blob(), np.uint8), cv2.IMREAD_UNCHANGED) if x.shape != (224, 224): return np.clip(x[..., [2, 1, 0]], 0, 255) # BGR to RGB else: # greyscale to RGB return np.clip(np.array([x, x, x]).transpose((1, 2, 0)), 0, 255)
Example #11
Source File: abstract_dataset.py From bonnet with GNU General Public License v3.0 | 6 votes |
def next_batch(self, size): ''' Return size items (wraps around if the last elements are less than a batch size. Be careful with this for evaluation) ''' # different if images are being buffered or not images = [] labels = [] names = [] if self.buff: for i in range(0, size): images.append(self.img_q.get()) # blocking labels.append(self.lbl_q.get()) # blocking names.append(self.name_q.get()) # blocking else: for i in range(0, size): img = cv2.imread(self.images[self.idx], cv2.IMREAD_UNCHANGED) lbl = cv2.imread(self.labels[self.idx], 0) images.append(img) labels.append(lbl) names.append(os.path.basename(self.images[self.idx])) self.idx += 1 if self.idx == self.num_examples: self.idx = 0 return images, labels, names
Example #12
Source File: utils.py From pytorch-serverless with MIT License | 6 votes |
def open_image(path): """ Opens an image using OpenCV given the file path. :param path: the file path of the image :return: the image in RGB format as numpy array of floats normalized to range between 0.0 - 1.0 """ flags = cv2.IMREAD_UNCHANGED+cv2.IMREAD_ANYDEPTH+cv2.IMREAD_ANYCOLOR path = str(path) if not os.path.exists(path): raise OSError(f'No such file or directory: {path}') elif os.path.isdir(path): raise OSError(f'Is a directory: {path}') else: try: im = cv2.imread(str(path), flags).astype(np.float32)/255 if im is None: raise OSError(f'File not recognized by opencv: {path}') return cv2.cvtColor(im, cv2.COLOR_BGR2RGB) except Exception as e: raise OSError(f'Error handling image at: {path}') from e
Example #13
Source File: util.py From real-world-sr with MIT License | 6 votes |
def read_img(env, path, size=None): '''read image by cv2 or from lmdb return: Numpy float32, HWC, BGR, [0,1]''' if env is None: # img img = cv2.imread(path, cv2.IMREAD_UNCHANGED) else: img = _read_img_lmdb(env, path, size) img = img.astype(np.float32) / 255. if img.ndim == 2: img = np.expand_dims(img, axis=2) # some images have 4 channels if img.shape[2] > 3: img = img[:, :, :3] return img #################### # image processing # process on numpy image ####################
Example #14
Source File: make_imagenet_c_inception.py From robustness with Apache License 2.0 | 6 votes |
def motion_blur(x, severity=1): c = [(12,4), (17,6), (17, 9), (17,13), (22,16)][severity - 1] output = BytesIO() x.save(output, format='PNG') x = MotionImage(blob=output.getvalue()) x.motion_blur(radius=c[0], sigma=c[1], angle=np.random.uniform(-45, 45)) x = cv2.imdecode(np.fromstring(x.make_blob(), np.uint8), cv2.IMREAD_UNCHANGED) if x.shape != (299, 299): return np.clip(x[..., [2, 1, 0]], 0, 255) # BGR to RGB else: # greyscale to RGB return np.clip(np.array([x, x, x]).transpose((1, 2, 0)), 0, 255)
Example #15
Source File: data_loaders.py From Pix2Vox with MIT License | 6 votes |
def get_datum(self, idx): taxonomy_name = self.file_list[idx]['taxonomy_name'] sample_name = self.file_list[idx]['sample_name'] rendering_image_path = self.file_list[idx]['rendering_image'] bounding_box = self.file_list[idx]['bounding_box'] volume_path = self.file_list[idx]['volume'] # Get data of rendering images rendering_image = cv2.imread(rendering_image_path, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255. if len(rendering_image.shape) < 3: print('[WARN] %s It seems the image file %s is grayscale.' % (dt.now(), rendering_image_path)) rendering_image = np.stack((rendering_image, ) * 3, -1) # Get data of volume with open(volume_path, 'rb') as f: volume = utils.binvox_rw.read_as_3d_array(f) volume = volume.data.astype(np.float32) return taxonomy_name, sample_name, np.asarray([rendering_image]), volume, bounding_box # //////////////////////////////// = End of Pascal3dDataset Class Definition = ///////////////////////////////// #
Example #16
Source File: data_loaders.py From Pix2Vox with MIT License | 6 votes |
def get_datum(self, idx): taxonomy_name = self.file_list[idx]['taxonomy_name'] sample_name = self.file_list[idx]['sample_name'] rendering_image_path = self.file_list[idx]['rendering_image'] bounding_box = self.file_list[idx]['bounding_box'] volume_path = self.file_list[idx]['volume'] # Get data of rendering images rendering_image = cv2.imread(rendering_image_path, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255. if len(rendering_image.shape) < 3: print('[WARN] %s It seems the image file %s is grayscale.' % (dt.now(), rendering_image_path)) rendering_image = np.stack((rendering_image, ) * 3, -1) # Get data of volume with open(volume_path, 'rb') as f: volume = utils.binvox_rw.read_as_3d_array(f) volume = volume.data.astype(np.float32) return taxonomy_name, sample_name, np.asarray([rendering_image]), volume, bounding_box # //////////////////////////////// = End of Pascal3dDataset Class Definition = ///////////////////////////////// #
Example #17
Source File: cnn_use_pb.py From bonnet with GNU General Public License v3.0 | 6 votes |
def predict_mask(img, sess, input, output, FLAGS, DATA): # open image cvim = cv2.imread(img, cv2.IMREAD_UNCHANGED) if cvim is None: print("No image to open for ", img) return # predict mask from image start = time.time() mask = sess.run(output, feed_dict={input: [cvim]}) print("Prediction for img ", img, ". Elapsed: ", time.time() - start, "s") # change to color color_mask = util.prediction_to_color( mask[0, :, :], DATA["label_remap"], DATA["color_map"]) cv2.imwrite(FLAGS.log + "/" + os.path.basename(img), color_mask) if FLAGS.verbose: # show me the image # first, mix with image im, transparent_mask = util.transparency(cvim, color_mask) all_img = np.concatenate((im, transparent_mask, color_mask), axis=1) util.im_tight_plt(all_img) util.im_block() return
Example #18
Source File: cnn_use_pb.py From bonnet with GNU General Public License v3.0 | 6 votes |
def predict_code(img, sess, input, output, FLAGS): # predict feature map from image # open image cvim = cv2.imread(img, cv2.IMREAD_UNCHANGED) if cvim is None: print("No image to open for ", img) return # predict code from image print("Prediction for img ", img) code = sess.run(output, feed_dict={input: [cvim]}) # reshape code to single dimension reshaped_code = np.reshape(code, (1, -1)) print("Shape", reshaped_code.shape) # save code to text file filename = FLAGS.log + "/" + \ os.path.splitext(os.path.basename(img))[0] + ".txt" print("Saving feature map to: ", filename) np.savetxt(filename, reshaped_code, fmt="%.8f", delimiter=" ") return
Example #19
Source File: cnn_use.py From bonnet with GNU General Public License v3.0 | 5 votes |
def predict_mask(img, net, FLAGS, DATA): # open image cvim = cv2.imread(img, cv2.IMREAD_UNCHANGED) if cvim is None: print("No image to open for ", img) return # predict mask from image start = time.time() mask = net.predict(cvim, path=FLAGS.path + '/' + FLAGS.model, verbose=FLAGS.verbose) print("Prediction for img ", img, ". Elapsed: ", time.time() - start, "s") # change to color color_mask = util.prediction_to_color( mask, DATA["label_remap"], DATA["color_map"]) # assess accuracy (if wanted) if FLAGS.label is not None: label = cv2.imread(FLAGS.label, 0) if label is None: print("No label to open") quit() net.individual_accuracy(mask, label) cv2.imwrite(FLAGS.log + "/" + os.path.basename(img), color_mask) if FLAGS.verbose: # show me the image # first, mix with image im, transparent_mask = util.transparency(cvim, color_mask) all_img = np.concatenate((im, transparent_mask, color_mask), axis=1) util.im_tight_plt(all_img) util.im_block() return
Example #20
Source File: text_list_adapter.py From lffd-pytorch with MIT License | 5 votes |
def get_one(self): """ This function use 'yield' to return samples """ while self.line_counter < len(self.lines): line = self.lines[self.line_counter].strip('\n').split(',') if line[1] == '1': # pos sample assert len(line[3:]) == 4 * int(line[2]) im = cv2.imread(line[0], cv2.IMREAD_UNCHANGED) if line[1] == '0': yield im, '0' self.line_counter += 1 continue num_bboxes = int(line[2]) bboxes = [] for i in range(num_bboxes): x = float(line[3 + i * 4]) y = float(line[3 + i * 4 + 1]) width = float(line[3 + i * 4 + 2]) height = float(line[3 + i * 4 + 3]) bboxes.append([x, y, width, height]) bboxes = numpy.array(bboxes, dtype=numpy.float32) yield im, bboxes self.line_counter += 1
Example #21
Source File: text_list_adapter.py From lffd-pytorch with MIT License | 5 votes |
def get_one(self): """ This function use 'yield' to return samples """ while self.line_counter < len(self.lines): line = self.lines[self.line_counter].strip('\n').split(',') if line[1] == '1': # 如果是正样本,需要校验bbox的个数是否一样 assert len(line[3:]) == 4 * int(line[2]) im = cv2.imread(line[0], cv2.IMREAD_UNCHANGED) if line[1] == '0': yield im, '0' self.line_counter += 1 continue num_bboxes = int(line[2]) bboxes = [] for i in range(num_bboxes): x = float(line[3 + i * 4]) y = float(line[3 + i * 4 + 1]) width = float(line[3 + i * 4 + 2]) height = float(line[3 + i * 4 + 3]) bboxes.append([x, y, width, height]) bboxes = numpy.array(bboxes, dtype=numpy.float32) yield im, bboxes self.line_counter += 1
Example #22
Source File: utils.py From QuickDraw with MIT License | 5 votes |
def get_images(path, classes): images = [cv2.imread("{}/{}.png".format(path, item), cv2.IMREAD_UNCHANGED) for item in classes] return images
Example #23
Source File: imgio_test.py From SickZil-Machine with GNU Affero General Public License v3.0 | 5 votes |
def test_load(): path = './fixture/not_proj_dir/bgr1_mask.png' expected_qimg = QImage(path) expected_ndarr= cv2.imread(path, cv2.IMREAD_UNCHANGED) assert io.load(path) == expected_qimg assert np.array_equal(io.load(path,io.NDARR),expected_ndarr)
Example #24
Source File: color2gray.py From IKC with Apache License 2.0 | 5 votes |
def worker(path, save_folder, mode, compression_level): img_name = os.path.basename(path) img = cv2.imread(path, cv2.IMREAD_UNCHANGED) # BGR if mode == 'gray': img_y = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) else: img_y = bgr2ycbcr(img, only_y=True) cv2.imwrite(os.path.join(save_folder, img_name), img_y, [cv2.IMWRITE_PNG_COMPRESSION, compression_level]) return 'Processing {:s} ...'.format(img_name)
Example #25
Source File: extract_subimgs_single.py From IKC with Apache License 2.0 | 5 votes |
def worker(path, save_folder, crop_sz, step, thres_sz, compression_level): img_name = os.path.basename(path) img = cv2.imread(path, cv2.IMREAD_UNCHANGED) n_channels = len(img.shape) if n_channels == 2: h, w = img.shape elif n_channels == 3: h, w, c = img.shape else: raise ValueError('Wrong image shape - {}'.format(n_channels)) h_space = np.arange(0, h - crop_sz + 1, step) if h - (h_space[-1] + crop_sz) > thres_sz: h_space = np.append(h_space, h - crop_sz) w_space = np.arange(0, w - crop_sz + 1, step) if w - (w_space[-1] + crop_sz) > thres_sz: w_space = np.append(w_space, w - crop_sz) index = 0 for x in h_space: for y in w_space: index += 1 if n_channels == 2: crop_img = img[x:x + crop_sz, y:y + crop_sz] else: crop_img = img[x:x + crop_sz, y:y + crop_sz, :] crop_img = np.ascontiguousarray(crop_img) # var = np.var(crop_img / 255) # if var > 0.008: # print(img_name, index_str, var) cv2.imwrite( os.path.join(save_folder, img_name.replace('.png', '_s{:03d}.png'.format(index))), crop_img, [cv2.IMWRITE_PNG_COMPRESSION, compression_level]) return 'Processing {:s} ...'.format(img_name)
Example #26
Source File: cv.py From droidbot with MIT License | 5 votes |
def load_image_from_buf(img_bytes): """ Load an image from a byte array :param img_bytes: The byte array of an image :return: """ import cv2 import numpy img_bytes = numpy.array(img_bytes) return cv2.imdecode(img_bytes, cv2.IMREAD_UNCHANGED)
Example #27
Source File: dataset.py From deep-photometric-stereo-network with MIT License | 5 votes |
def load_data(self, root_path): def_png_path = os.path.join(root_path, "{light_index}.png") m, n = self.img_size M = np.zeros(shape=(m * n, self.light_num, 3), dtype=np.float32) for l in range(self.light_num): m_img = cv2.imread(def_png_path.format(light_index=l), cv2.IMREAD_UNCHANGED)[:, :, ::-1] # m_img = cv2.imread(def_png_path.format(light_index=l))[:, :, ::-1] M[:, l, :] = m_img.reshape(-1, 3) obj_name, brdf_name = self.data_path2name(root_path + "/") N, m, n, mask = self.__load_normal_png(os.path.join(self.dataset_path, obj_name, "{}.png".format(obj_name))) return M, N, mask
Example #28
Source File: codecs.py From petastorm with Apache License 2.0 | 5 votes |
def decode(self, unischema_field, value): """Decodes the image using OpenCV.""" # cv returns a BGR or grayscale image. Convert to RGB (unless a grayscale image). image_bgr_or_gray = cv2.imdecode(np.frombuffer(value, dtype=np.uint8), cv2.IMREAD_UNCHANGED) if len(image_bgr_or_gray.shape) == 2: # Greyscale image return image_bgr_or_gray elif len(image_bgr_or_gray.shape) == 3 and image_bgr_or_gray.shape[2] == 3: # Convert BGR to RGB (opencv assumes BGR) image_rgb = image_bgr_or_gray[:, :, (2, 1, 0)] return image_rgb else: raise ValueError('Unexpected image dimensions. Supported dimensions are (H, W) or (H, W, 3). ' 'Got {}'.format(image_bgr_or_gray.shape))
Example #29
Source File: test_transform_set_select_curate_flask_app.py From deepstar with BSD 3-Clause Clear License | 5 votes |
def test_transform_get(self): with deepstar_path(): self.setUp_() api = TransformSetSelectCurateFlaskApp(1).api.app.test_client() response = api.get('/transform_sets/1/transforms/1') self.assertEqual(response.status_code, 200) self.assertIsInstance(cv2.imdecode(np.fromstring(response.get_data(), dtype=np.uint8), cv2.IMREAD_UNCHANGED), np.ndarray) # noqa
Example #30
Source File: test_transform_set_select_curate_flask_app.py From deepstar with BSD 3-Clause Clear License | 5 votes |
def test_static(self): with deepstar_path(): self.setUp_() api = TransformSetSelectCurateFlaskApp(1).api.app.test_client() response = api.get('/static/img/checkmark.png') self.assertEqual(response.status_code, 200) self.assertIsInstance(cv2.imdecode(np.fromstring(response.get_data(), dtype=np.uint8), cv2.IMREAD_UNCHANGED), np.ndarray) # noqa response = api.get('/static/img/xmark.png') self.assertEqual(response.status_code, 200) self.assertIsInstance(cv2.imdecode(np.fromstring(response.get_data(), dtype=np.uint8), cv2.IMREAD_UNCHANGED), np.ndarray) # noqa