def load_heatmaps(self, name):
heatmaps = []
for step in range(self.step_total):
try:
file_name = '../../'+self.data_base+'/'+name+'/'+self.env_id+'_'+str(step)+'.jpg'
temp = cv2.imread(file_name, cv2.CV_LOAD_IMAGE_GRAYSCALE)
temp = cv2.resize(temp,(self.heatmap_width, self.heatmap_height))
temp = temp / 255.0
heatmaps += [temp]
except Exception,e:
print Exception,":",e
continue
print('load heatmaps: '+name+' done, size: '+str(np.shape(heatmaps)))
return heatmaps
python类CV_LOAD_IMAGE_GRAYSCALE的实例源码
def test_read_mnist(self):
"""
Tests reading from the MNIST LMDB.
"""
lmdb_path = 'tests/mnist_test_lmdb'
lmdb = tools.lmdb_io.LMDB(lmdb_path)
keys = lmdb.keys(5)
for key in keys:
image, label, key = lmdb.read(key)
image_path = 'tests/mnist_test/' + key + '.png'
assert os.path.exists(image_path)
image = cv2.imread(image_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
self.assertEqual(image[i, j], image[i, j])
def main(input_pic):
img = cv.imread(input_pic,cv.CV_LOAD_IMAGE_GRAYSCALE)
img=sp.gaussian_filter(img,sigma=3)
img= imresize(img,((len(img)/10),(len(img[0])/10)))
img_arr=np.asarray(img,dtype="int32")
LoG_arr=LoG_Filter(img_arr)
cv.imwrite('LoG_image.jpg',LoG_arr)
LoG_arr=cv.imread('LoG_image.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
Hist=genHistogram(LoG_arr)
#print(Hist)
for i in range(0,len(LoG_arr)):
for j in range(0,len(LoG_arr[0])):
if LoG_arr[i][j]<200:
LoG_arr[i][j]=0
else:
LoG_arr[i][j]=255
cv.imwrite('LoG_image.jpg',LoG_arr)
#img_new=cv.imread('LoG_image.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
def generate_hdf5(data, output='shit.h5'):
lines = []
dst = 'tf_test/'
imgs = []
labels = []
for (imgPath, bbx, landmarks) in data:
im = cv2.imread(imgPath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
imgName = imgPath.split('/')[-1][:-4]
bbx_sc = bbx.bbxScale(im.shape, scale=1.1)
#print bbx_sc.x, bbx_sc.y, bbx_sc.w, bbx_sc.h
im_sc = im[bbx_sc.y:bbx_sc.y+bbx_sc.h, bbx_sc.x:bbx_sc.x+bbx_sc.w]
im_sc = cv2.resize(im_sc, (39, 39))
imgs.append(im_sc.reshape(39, 39, 1))
name = dst+imgName+'sc.jpg'
lm_sc = bbx_sc.normalizeLmToBbx(landmarks)
labels.append(lm_sc.reshape(10))
lines.append(name + ' ' + ' '.join(map(str, lm_sc.flatten())) + '\n')
imgs, labels = np.asarray(imgs), np.asarray(labels)
imgs = processImage(imgs)
with h5py.File('shit.h5', 'w') as h5:
h5['data'] = imgs.astype(np.float32)
h5['landmark'] = labels.astype(np.float32)
def E():
data = getDataFromTxt(TXT)
error = np.zeros((len(data), 3))
for i in range(len(data)):
imgPath, bbox, landmarkGt = data[i]
landmarkGt = landmarkGt[2:, :]
img = cv2.imread(imgPath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
assert(img is not None)
logger("process %s" % imgPath)
landmarkP = NM(img, bbox)
# real landmark
landmarkP = bbox.reprojectLandmark(landmarkP)
landmarkGt = bbox.reprojectLandmark(landmarkGt)
error[i] = evaluateError(landmarkGt, landmarkP, bbox)
return error
def E():
data = getDataFromTxt(TXT)
error = np.zeros((len(data), 3))
for i in range(len(data)):
imgPath, bbox, landmarkGt = data[i]
landmarkGt = landmarkGt[:3, :]
img = cv2.imread(imgPath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
assert(img is not None)
logger("process %s" % imgPath)
landmarkP = EN(img, bbox)
# real landmark
landmarkP = bbox.reprojectLandmark(landmarkP)
landmarkGt = bbox.reprojectLandmark(landmarkGt)
error[i] = evaluateError(landmarkGt, landmarkP, bbox)
return error
def E():
data = getDataFromTxt(TXT)
error = np.zeros((len(data), 5))
for i in range(len(data)):
imgPath, bbox, landmarkGt = data[i]
img = cv2.imread(imgPath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
assert(img is not None)
logger("process %s" % imgPath)
landmarkP = getResult(img, bbox)
# real landmark
landmarkP = bbox.reprojectLandmark(landmarkP)
landmarkGt = bbox.reprojectLandmark(landmarkGt)
error[i] = evaluateError(landmarkGt, landmarkP, bbox)
return error
def E():
data = getDataFromTxt(TXT)
error = np.zeros((len(data), 5))
for i in range(len(data)):
imgPath, bbox, landmarkGt = data[i]
img = cv2.imread(imgPath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
assert(img is not None)
logger("process %s" % imgPath)
landmarkP = getResult(img, bbox)
# real landmark
landmarkP = bbox.reprojectLandmark(landmarkP)
landmarkGt = bbox.reprojectLandmark(landmarkGt)
error[i] = evaluateError(landmarkGt, landmarkP, bbox)
return error
def decode(self, msg):
fn = os.path.join(self.directory_, msg)
if os.path.exists(fn):
im = cv2.imread(fn,
cv2.CV_LOAD_IMAGE_COLOR if self.color_ \
else cv2.CV_LOAD_IMAGE_GRAYSCALE)
return im_resize(im, shape=self.shape_)
else:
raise Exception('File does not exist')
# Basic type for image annotations
def format_image(image):
if len(image.shape) > 2 and image.shape[2] == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
image = cv2.imdecode(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
faces = cascade_classifier.detectMultiScale(
image,
scaleFactor = 1.3,
minNeighbors = 5
)
# None is we don't found an image
if not len(faces) > 0:
return None
max_area_face = faces[0]
for face in faces:
if face[2] * face[3] > max_area_face[2] * max_area_face[3]:
max_area_face = face
# Chop image to face
face = max_area_face
image = image[face[1]:(face[1] + face[2]), face[0]:(face[0] + face[3])]
# Resize image to network size
try:
image = cv2.resize(image, (SIZE_FACE, SIZE_FACE), interpolation = cv2.INTER_CUBIC) / 255.
except Exception:
print("[+] Problem during resize")
return None
# cv2.imshow("Lol", image)
# cv2.waitKey(0)
return image
# Load Model
def load_image_vector(img_path):
try:
M = cv2.imread(img_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
except:
read_pgm(img_path)
M = cv2.resize(M, (IM_WIDTH, IM_HEIGHT))
M = M.reshape((1, IM_AREA))
return M[0]
def format_image(image):
"""
Function to format frame
"""
if len(image.shape) > 2 and image.shape[2] == 3:
# determine whether the image is color
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
# Image read from buffer
image = cv2.imdecode(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
faces = cascade_classifier.detectMultiScale(image,scaleFactor = 1.3 ,minNeighbors = 5)
if not len(faces) > 0:
return None
# initialize the first face as having maximum area, then find the one with max_area
max_area_face = faces[0]
for face in faces:
if face[2] * face[3] > max_area_face[2] * max_area_face[3]:
max_area_face = face
face = max_area_face
# extract ROI of face
image = image[face[1]:(face[1] + face[2]), face[0]:(face[0] + face[3])]
try:
# resize the image so that it can be passed to the neural network
image = cv2.resize(image, (48,48), interpolation = cv2.INTER_CUBIC) / 255.
except Exception:
print("----->Problem during resize")
return None
return image
# Initialize object of EMR class
def get_full_size_labels(self, img_ids, timespan=None):
"""Get full sized labels."""
if timespan is None:
timespan = self.get_default_timespan()
with h5py.File(self.h5_fname, 'r') as h5f:
num_ex = len(img_ids)
y_full = []
for kk, ii in enumerate(img_ids):
key = self.get_str_id(ii)
data_group = h5f[key]
if 'label_segmentation_full_size' in data_group:
y_gt_group = data_group['label_segmentation_full_size']
num_obj = len(y_gt_group.keys())
y_full_kk = None
for jj in xrange(min(num_obj, timespan)):
y_full_jj_str = y_gt_group['{:02d}'.format(jj)][:]
y_full_jj = cv2.imdecode(
y_full_jj_str, cv2.CV_LOAD_IMAGE_GRAYSCALE).astype('float32')
if y_full_kk is None:
y_full_kk = np.zeros(
[timespan, y_full_jj.shape[0], y_full_jj.shape[1]])
y_full_kk[jj] = y_full_jj
y_full.append(y_full_kk)
else:
y_full.append(np.zeros([timespan] + list(data_group['orig_size'][:])))
return y_full
def get_full_size_labels(self, img_ids, timespan=None):
"""Get full sized labels."""
if timespan is None:
timespan = self.get_default_timespan()
with h5py.File(self.h5_fname, "r") as h5f:
num_ex = len(img_ids)
y_full = []
for kk, ii in enumerate(img_ids):
key = self.get_str_id(ii)
data_group = h5f[key]
if "label_ins_seg_full" in data_group:
y_gt_group = data_group["label_ins_seg_full"]
num_obj = len(y_gt_group.keys())
y_full_kk = None
for jj in range(min(num_obj, timespan)):
y_full_jj_str = y_gt_group["{:03d}".format(jj)][:]
y_full_jj = cv2.imdecode(
y_full_jj_str, cv2.CV_LOAD_IMAGE_GRAYSCALE).astype(np.float32)
if y_full_kk is None:
y_full_kk = np.zeros(
[timespan, y_full_jj.shape[0], y_full_jj.shape[1]])
y_full_kk[jj] = y_full_jj
y_full.append(y_full_kk)
else:
y_full.append(np.zeros([timespan] + list(data_group["orig_size"][:])))
return y_full
def read_pgm(filename):
img1 = cv2.imread(filename, cv2.CV_LOAD_IMAGE_GRAYSCALE)
h, w = img1.shape[:2]
vis0 = np.zeros((h,w), np.float32)
vis0[:h, :w] = img1
return vis0
#This method is used to read cover and stego images.
#We consider that stego images can be steganographied with differents keys (in practice this seems to be inefficient...)
def calculate_feature(bin_data):
"""
calculate the feature data of an image
parameter :
'bin_data' is the binary stream format of an image
return value :
a tuple of ( keypoints, descriptors, (height,width) )
keypoints is like [ pt1, pt2, pt3, ... ]
descriptors is a numpy array
"""
buff=numpy.frombuffer(bin_data,numpy.uint8)
img_obj=cv2.imdecode(buff,cv2.CV_LOAD_IMAGE_GRAYSCALE)
surf=cv2.FeatureDetector_create("SURF")
surf.setInt("hessianThreshold",400)
surf_extractor=cv2.DescriptorExtractor_create("SURF")
keypoints=surf.detect(img_obj,None)
keypoints,descriptors=surf_extractor.compute(img_obj,keypoints)
res_keypoints=[]
for point in keypoints:
res_keypoints.append(point.pt)
del buff
del surf
del surf_extractor
del keypoints
return res_keypoints,numpy.array(descriptors),img_obj.shape
registration.py 文件源码
项目:Kinect-ASUS-Xtion-Pro-Live-Calibration-Tutorials
作者: taochenshh
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def __init__(self):
self.br = CvBridge()
# If you subscribe /camera/depth_registered/hw_registered/image_rect topic, the depth image and rgb image are
# already registered. So you don't need to call register_depth_to_rgb()
# self.depth_image_sub = rospy.Subscriber("/camera/depth_registered/hw_registered/image_rect",Image,self.depth_callback)
self.depth_image_sub = rospy.Subscriber("/camera/depth/image_rect",Image,self.depth_callback)
self.rgb_image_sub = rospy.Subscriber("/camera/rgb/image_rect_color",Image,self.rgb_callback)
self.ir_img = None
self.rgb_img = None
self.rgb_rmat = None
self.rgb_tvec = None
self.ir_rmat = None
self.ir_tvec = None
self.ir_to_rgb_rmat = None
self.ir_to_rgb_tvec = None
self.depth_image = None
self.rgb_image = None
self.load_extrinsics()
self.load_intrinsics()
self.depth_image = None
self.rgb_image = None
self.count = 0
# self.depth_image = cv2.imread("/home/chentao/depth.png", cv2.CV_LOAD_IMAGE_GRAYSCALE)
# self.rgb_image = cv2.imread("/home/chentao/rgb.png")
def convert_to_binary_image(img_path, preview):
img = cv2.imread(img_path)
if preview: _preview_image("Original Message Image", img, keep_open=True)
img_gray = cv2.imread(img_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
if preview: _preview_image("Gray Scale Message Image", img_gray, keep_open=True)
(thresh, img_bw) = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
if preview: _preview_image("Black & White Message Image", img_bw)
return img_bw
def compute(i):
img_file = files[i]
img = cv2.imread(img_file, cv2.CV_LOAD_IMAGE_GRAYSCALE)
if img is None:
print 'img {} is None, path correct? --> skip'.format(img_file)
return
kpts = fe.detect(img)
_, descriptors = fe.extract(img, kpts)
if descriptors is None or len(descriptors) == 0:
print 'WARNING: no descriptors extracted, skip image', img_file
sys.exit(1)
# Hellinger normalization
descriptors += np.finfo(np.float32).eps
descriptors /= np.sum(descriptors, axis=1)[:,np.newaxis]
descriptors = np.sqrt(descriptors)
# output
new_basename = os.path.join(args.outputfolder,
os.path.basename(os.path.splitext(img_file)[0]))
feat_filename = new_basename + '_' + args.detector \
+ '_' + args.feature + '.pkl.gz'
with gzip.open(feat_filename, 'wb') as f:
cPickle.dump(descriptors, f, -1)
progress.update(i+1)
def extract_image(filename, resize_height, resize_width):
filename1 = 'train/'+filename+'.jpeg'
image = cv2.imread(filename1,cv2.CV_LOAD_IMAGE_GRAYSCALE)
#image = cv2.imread(filename1)
image = cv2.resize(image, (resize_height, resize_width))
#b,g,r = cv2.split(image)
#rgb_image = cv2.merge([r,g,b])
cv2.imwrite(filename+'.jpeg', image)
return image
def main(img_dir, output_dir, pretrained_phocnet, deploy_proto, min_image_width_height, gpu_id):
logging_format = '[%(asctime)-19s, %(name)s, %(levelname)s] %(message)s'
logging.basicConfig(level=logging.INFO,
format=logging_format)
logger = logging.getLogger('Predict PHOCs')
if gpu_id is None:
caffe.set_mode_cpu()
else:
caffe.set_mode_gpu()
caffe.set_device(gpu_id)
logger.info('Loading PHOCNet...')
phocnet = caffe.Net(deploy_proto, caffe.TEST, weights=pretrained_phocnet)
# find all images in the supplied dir
logger.info('Found %d word images to process', len(os.listdir(img_dir)))
word_img_list = [cv2.imread(os.path.join(img_dir, filename), cv2.CV_LOAD_IMAGE_GRAYSCALE)
for filename in sorted(os.listdir(img_dir)) if filename not in ['.', '..']]
# push images through the PHOCNet
logger.info('Predicting PHOCs...')
predicted_phocs = net_output_for_word_image_list(phocnet=phocnet, word_img_list=word_img_list,
min_img_width_height=min_image_width_height)
# save everything
logger.info('Saving...')
np.save(os.path.join(output_dir, 'predicted_phocs.npy'), predicted_phocs)
logger.info('Finished')
def get_word_image(self, gray_scale=True):
col_type = None
if gray_scale:
col_type = cv2.CV_LOAD_IMAGE_GRAYSCALE
else:
col_type = cv2.CV_LOAD_IMAGE_COLOR
# load the image
ul = self.bounding_box['upperLeft']
wh = self.bounding_box['widthHeight']
img = cv2.imread(self.image_path, col_type)
if not np.all(self.bounding_box['widthHeight'] == -1):
img = img[ul[1]:ul[1]+wh[1], ul[0]:ul[0]+wh[0]]
return img
def format_image(image):
if len(image.shape) > 2 and image.shape[2] == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
image = cv2.imdecode(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
faces = cascade_classifier.detectMultiScale(
image,
scaleFactor=1.3,
minNeighbors=5
)
if not len(faces) > 0:
return None
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(frame, str(curr_emotion), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255))
max_area_face = faces[0]
for face in faces:
if face[2] * face[3] > max_area_face[2] * max_area_face[3]:
max_area_face = face
face = max_area_face
image = image[face[1]:(face[1] + face[2]), face[0]:(face[0] + face[3])]
try:
image = cv2.resize(image, (48, 48), interpolation=cv2.INTER_CUBIC) / 255.
except Exception:
print("[+] Problem during resize")
return None
return image
def im_from_file(f):
a = numpy.asarray(bytearray(f.read()), dtype=numpy.uint8)
return cv2.imdecode(a, cv2.CV_LOAD_IMAGE_GRAYSCALE)
def generate_bg(num_bg_images):
found = False
while not found:
fname = "bgs/{:08d}.jpg".format(random.randint(0, num_bg_images - 1))
bg = cv2.imread(fname, cv2.CV_LOAD_IMAGE_GRAYSCALE) / 255.
if (bg.shape[1] >= OUTPUT_SHAPE[1] and
bg.shape[0] >= OUTPUT_SHAPE[0]):
found = True
x = random.randint(0, bg.shape[1] - OUTPUT_SHAPE[1])
y = random.randint(0, bg.shape[0] - OUTPUT_SHAPE[0])
bg = bg[y:y + OUTPUT_SHAPE[0], x:x + OUTPUT_SHAPE[1]]
return bg
def manuallySegmentDisparities():
# Define Source Directories
src_dir_anno = '../data/img/terra/405late_20161011194413_3_116_lb'
src_dir_left = '/media/paloma/Data1/Linux_Data/TERRA/texas_field_tests/20161011/CS_405late_2016-10-11-19-44-13_PIF3_116_lb/qc_l_tr/rectified'
src_dir_right = '/media/paloma/Data1/Linux_Data/TERRA/texas_field_tests/20161011/CS_405late_2016-10-11-19-44-13_PIF3_116_lb/qc_r_tl/rectified'
# Read Source File Paths into alist
src_xmlfiles = collectFilePaths(src_dir_anno, '.xml')
src_imgfiles = collectFilePaths(src_dir_anno, '.jpg')
src_imgfiles_left = collectFilePaths(src_dir_left, '.jpg')
src_imgfiles_right = collectFilePaths(src_dir_right, '.jpg')
# Source Image Checks
assert (len(src_xmlfiles) == len(src_imgfiles)), "number of image and annotation files should be equal"
assert (len(src_imgfiles_left) == len(src_imgfiles_right)), "number of left and right images should be equal"
# Objects and Classes being called
stemXMLParser = XMLParser('stem')
dispComputer = DisparityComputer()
comImgOps = CommonImageOperations()
# Define Destination Directories
dest_img_left = '/home/paloma/code/OpenCVReprojectImageToPointCloud/CS_405late_2016-10-11-19-44-13_PIF3_116_lb/rgb-image-'
dest_disp = '/home/paloma/code/OpenCVReprojectImageToPointCloud/CS_405late_2016-10-11-19-44-13_PIF3_116_lb/disparity-image-'
file_idx = 0
for (xmlfile, imgfile, imgfile_right) in zip(src_xmlfiles, src_imgfiles, src_imgfiles_right):
print 'File Idx : ' + str(file_idx)
xmlroot = (ET.parse(xmlfile)).getroot()
img = cv2.imread(imgfile)
img_left = cv2.imread(imgfile, cv2.CV_LOAD_IMAGE_GRAYSCALE)
img_right = cv2.imread(imgfile_right, cv2.CV_LOAD_IMAGE_GRAYSCALE)
mask_stem = stemXMLParser.getLabelMask(img, xmlroot)
(disp_left, disp_left_fgnd, img_fgnd) = dispComputer.getDisparity(img_left, img_right)
img_left = comImgOps.cropImage(img_left, numrows_crop=45, numcols_crop=35)
disp_left = comImgOps.cropImage(disp_left, numrows_crop=45, numcols_crop=35)
mask_stem = comImgOps.cropImage(mask_stem, numrows_crop=45, numcols_crop=35)
cv2.imwrite(dest_img_left+str(file_idx)+'.ppm', img_left*mask_stem[:,:,1])
cv2.imwrite(dest_disp+str(file_idx)+'.pgm', disp_left*mask_stem[:,:,1])
file_idx = file_idx + 1