def get_points():
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*8,3), np.float32)
objp[:,:2] = np.mgrid[0:8, 0:6].T.reshape(-1 , 2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
# Make a list of calibration images
images = glob.glob('calibration_wide/GO*.jpg')
# Step through the list and search for chessboard corners
for idx, fname in enumerate(images):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (8,6), None)
# If found, add object points, image points
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(img, (8,6), corners, ret)
#write_name = 'corners_found'+str(idx)+'.jpg'
#cv2.imwrite(write_name, img)
cv2.imshow('img', img)
cv2.waitKey(500)
cv2.destroyAllWindows()
return objpoints, imgpoints
python类imread()的实例源码
def main():
# reading in an image
#image = (mpimg.imread('test_images/solidWhiteRight.jpg') * 255).astype('uint8')
#image = (mpimg.imread('test_images/solidWhiteCurve.jpg') * 255).astype('uint8')
#image = (mpimg.imread('test_images/solidYellowCurve.jpg') * 255).astype('uint8')
#image = (mpimg.imread('test_images/solidYellowCurve2.jpg') * 255).astype('uint8')
#image = (mpimg.imread('test_images/solidYellowLeft.jpg') * 255).astype('uint8')
image = (mpimg.imread('test_images/whiteCarLaneSwitch.jpg') * 255).astype('uint8')
processImage = process_image(image)
plt.imshow(processImage)
plt.show()
# Make video
white_output = 'white.mp4'
clip1 = VideoFileClip("solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
white_clip.write_videofile(white_output, audio=False)
# Make video
yellow_output = 'yellow.mp4'
clip2 = VideoFileClip('solidYellowLeft.mp4')
yellow_clip = clip2.fl_image(process_image)
yellow_clip.write_videofile(yellow_output, audio=False)
def save_rotated_test_images():
#
# DESCRIPTION
# This function rotates the test image and create four patches of 400 * 400
# It then saves those 32 images in the test_set_images folder of each image
#
#
# Loop over all images
for i in range(1,51):
# Load image
image = mpimg.imread('test_set_images/test_'+str(i)+'/test_'+str(i)+'.png')
rotations = mk_rotations(image)
rota_count = 0
for rotation in rotations:
patches = make_4_patch(rotation)
patch_count = 0
for patch in patches:
patch = format_image(patch)
Image.fromarray(patch).save('test_set_images/test_'+str(i)+'/Test_'+str(i)+'_rota'+str(rota_count)+'_patch'+str(patch_count)+'.png')
patch_count += 1
rota_count+=1
print('Writing image ',i)
def extract_labels(filename, num_images):
gt_imgs = []
for i in range(1, num_images+1):
imageid = 'training_big/Truth/satImage_'+ '%.3d' % i
for j in range(8):
image_filename = imageid + "_rota"+str(np.int(j))+".png"
if os.path.isfile(image_filename):
img = mpimg.imread(image_filename)
gt_imgs.append(img)
else:
print ('File ' + image_filename + ' does not exist')
num_images = len(gt_imgs)
gt_patches = [img_crop(gt_imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)]
data = np.asarray([gt_patches[i][j] for i in range(len(gt_patches)) for j in range(len(gt_patches[i]))])
labels = np.asarray([value_to_class(np.mean(data[i])) for i in range(len(data))])
# Convert to dense 1-hot representation.
return labels.astype(np.float32)
##Return the error rate based on dense predictions and 1-hot labels.
def load_stereo_pairs(imL_files, imR_files, **kwargs):
"""Helper method to read stereo image pairs."""
StereoPair = namedtuple('StereoPair', 'left, right')
impairs = []
for imfiles in zip(imL_files, imR_files):
# Convert to uint8 and BGR for OpenCV if requested
imformat = kwargs.get('format', '')
if imformat is 'cv2':
imL = np.uint8(mpimg.imread(imfiles[0]) * 255)
imR = np.uint8(mpimg.imread(imfiles[1]) * 255)
# Convert RGB to BGR
if len(imL.shape) > 2:
imL = imL[:, :, ::-1]
imR = imR[:, :, ::-1]
else:
imL = mpimg.imread(imfiles[0])
imR = mpimg.imread(imfiles[1])
impairs.append(StereoPair(imL, imR))
return impairs
def create_thumbnail(infile, thumbfile,
width=300, height=300,
cx=0.5, cy=0.5, border=4):
baseout, extout = op.splitext(thumbfile)
im = image.imread(infile)
rows, cols = im.shape[:2]
x0 = int(cx * cols - .5 * width)
y0 = int(cy * rows - .5 * height)
xslice = slice(x0, x0 + width)
yslice = slice(y0, y0 + height)
thumb = im[yslice, xslice]
thumb[:border, :, :3] = thumb[-border:, :, :3] = 0
thumb[:, :border, :3] = thumb[:, -border:, :3] = 0
dpi = 100
fig = plt.figure(figsize=(width / dpi, height / dpi), dpi=dpi)
ax = fig.add_axes([0, 0, 1, 1], aspect='auto',
frameon=False, xticks=[], yticks=[])
ax.imshow(thumb, aspect='auto', resample=True,
interpolation='bilinear')
fig.savefig(thumbfile, dpi=dpi)
return fig
def load_stereo_pairs(imL_files, imR_files, **kwargs):
"""Helper method to read stereo image pairs."""
StereoPair = namedtuple('StereoPair', 'left, right')
impairs = []
for imfiles in zip(imL_files, imR_files):
# Convert to uint8 and BGR for OpenCV if requested
imformat = kwargs.get('format', '')
if imformat is 'cv2':
imL = np.uint8(mpimg.imread(imfiles[0]) * 255)
imR = np.uint8(mpimg.imread(imfiles[1]) * 255)
# Convert RGB to BGR
if len(imL.shape) > 2:
imL = imL[:, :, ::-1]
imR = imR[:, :, ::-1]
else:
imL = mpimg.imread(imfiles[0])
imR = mpimg.imread(imfiles[1])
impairs.append(StereoPair(imL, imR))
return impairs
def graphviz_plot(graph, fname="tmp_dotgraph.dot", show=True):
if os.path.exists(fname):
print("WARNING: Overwriting existing file {} for new plots".format(fname))
f = open(fname,'w')
f.writelines('digraph G {\nnode [width=.3,height=.3,shape=octagon,style=filled,color=skyblue];\noverlap="false";\nrankdir="LR";\n')
for i in graph:
for j in graph[i]:
s= ' '+ i
s += ' -> ' + j + ' [label="' + str(graph[i][j]) + '"]'
s+=';\n'
f.writelines(s)
f.writelines('}')
f.close()
graphname = fname.split(".")[0] + ".png"
pe(["dot", "-Tpng", fname, "-o", graphname])
if show:
plt.imshow(mpimg.imread(graphname))
plt.show()
def exportPlot(self):
# Combine to one
self.plot_output_file, filetype = QtWidgets.QFileDialog.getSaveFileName(self, "Export Plots To...", "", "PDF(*.pdf)")
if filetype == "PDF(*.pdf)":
pdf = matplotlib.backends.backend_pdf.PdfPages(self.plot_output_file)
for key in list(self.plot_file.keys()):
for i in range(1, len(self.plot_file[key])):
fig = plt.figure()
img = mpimg.imread(self.plot_file[key][i])
plt.imshow(img)
plt.axis('off')
pdf.savefig(fig)
plt.clf()
plt.close()
pdf.close()
sys.stdout.write("Output plot files to %s"%(self.plot_output_file))
self.exportPlot_flag = 1
def run(self):
if self.args is None:
args = []
else:
args = self.args
if self.kwargs is None:
kwargs = {}
else:
kwargs = self.kwargs
comp_imgs = []
tmpdir = tempfile.mkdtemp()
image_prefix = os.path.join(tmpdir,"test_img")
self.image_func(image_prefix, *args, **kwargs)
imgs = glob.glob(image_prefix+"*")
assert(len(imgs) > 0)
for img in imgs:
img_data = mpimg.imread(img)
os.remove(img)
comp_imgs.append(zlib.compress(img_data.dumps()))
return comp_imgs
def imscatter(x, y, image, ax=None, zoom=1):
if ax is None:
ax = plt.gca()
try:
image = plt.imread(image)
except TypeError:
# Likely already an array...
pass
im = OffsetImage(image, zoom=zoom)
x, y = np.atleast_1d(x, y)
artists = []
for x0, y0 in zip(x, y):
ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=False)
artists.append(ax.add_artist(ab))
ax.update_datalim(np.column_stack([x, y]))
ax.autoscale()
return artists
def plot(error_index, dataset_path):
img = mpimg.imread(dataset_path)
plt.imshow(img)
currentAxis = plt.gca()
for index in error_index:
row = index // 2
column = index % 2
currentAxis.add_patch(
patches.Rectangle(
xy=(
47 * 9 if column == 0 else 47 * 19,
row * 57
),
width=47,
height=57,
linewidth=1,
edgecolor='r',
facecolor='none'
)
)
fig = plt.gcf()
fig.set_size_inches(11.40, 9.42)
plt.savefig("fig_result.png", bbox_inches="tight", dpi=100)
plt.show()
def print_images(output, img_path):
seg_path = img_path.replace("jpg", "png")
out_img = np.uint8(np.squeeze(output.asnumpy().argmax(axis=1)))
out_img = Image.fromarray(out_img)
out_img.putpalette(getpallete(256))
out_img.save(seg_path)
# Display input
print "Input Image:"
img = mpimg.imread(img_path)
plt.imshow(img)
plt.show()
# Display output
print "Output Image:"
img_out = mpimg.imread(seg_path)
plt.imshow(img_out)
plt.show()
def by_file_name(self, image_file_name):
"""
:param image_file_name:
:return:
image from file (or from cache)
empty image if image_file_name empty or file does not exists
"""
if not image_file_name:
return self._empty
if image_file_name not in self._cache:
try:
self._cache[image_file_name] = mpimg.imread(image_file_name)
except Exception as e:
print('#'*5, ' Error reading image from {}:\n{}'.format(image_file_name, e))
return self._empty
return self._cache[image_file_name]
def extract_data(filename, num_images):
"""Extract the images into a 4D tensor [image index, y, x, channels].
Values are rescaled from [0, 255] down to [-0.5, 0.5].
"""
imgs = []
for i in range(1, num_images + 1):
imageid = "satImage_%.3d" % i
image_filename = filename + imageid + ".png"
if os.path.isfile(image_filename):
print('Loading ' + image_filename)
img = mpimg.imread(image_filename)
imgs.append(img)
else:
print('File ' + image_filename + ' does not exist')
num_images = len(imgs)
img_patches = [img_crop(imgs[i], IMG_PATCH_SIZE, IMG_PATCH_SIZE) for i in range(num_images)]
data = [img_patches[i][j] for i in range(len(img_patches)) for j in range(len(img_patches[i]))]
return numpy.asarray(data)
# Assign a label to a patch v
def extract_labels(filename_base, num_images, num_of_transformations=6, patch_size=const.IMG_PATCH_SIZE,
patch_stride=const.IMG_PATCH_STRIDE):
"""Extract the labels into a 1-hot matrix [image index, label index]."""
gt_imgs = []
for i in range(1, num_images+1):
imageid = "satImage_%.3d" % i
image_filename = filename_base + imageid + ".png"
if os.path.isfile(image_filename):
print('Loading ' + image_filename)
img = mpimg.imread(image_filename)
gt_imgs.append(img)
else:
print('File ' + image_filename + ' does not exist')
num_images = len(gt_imgs)
print('Extracting patches...')
gt_patches = [pem.label_img_crop(gt_imgs[i], patch_size, patch_stride, num_of_transformations)
for i in range(num_images)]
data = np.asarray([gt_patches[i][j] for i in range(len(gt_patches)) for j in range(len(gt_patches[i]))])
labels = np.asarray([value_to_class(np.mean(data[i])) for i in range(len(data))])
print(str(len(data)) + ' label patches extracted.')
# Convert to dense 1-hot representation.
return labels.astype(np.float32)
def extract_label_images(filename_base, num_images, patch_size=const.IMG_PATCH_SIZE,
patch_stride=const.IMG_PATCH_STRIDE, img_base_name="satImage_%.3d"):
"""Extract labels from ground truth as label images."""
gt_imgs = []
for i in range(1, num_images+1):
imageid = img_base_name % i
image_filename = filename_base + imageid + ".png"
if os.path.isfile(image_filename):
print('Loading ' + image_filename)
img = mpimg.imread(image_filename)
gt_imgs.append(img)
else:
print('File ' + image_filename + ' does not exist')
num_images = len(gt_imgs)
print('Extracting patches...')
gt_patches = [pixel_to_patch_labels(gt_imgs[i], patch_size, patch_stride) for i in range(num_images)]
return gt_patches
def filter_bad_images():
index = 0
gt_index = arguments.gt_start
predicted_index = arguments.predicted_start
indices = []
filter_path = search(predicted_index)
filter_filename = os.path.join(arguments.gt_path, "filter_close.txt")
if os.path.exists(filter_filename):
with open(filter_filename, "r") as filter_file:
filter_close_indices = [int(line.strip()) for line in filter_file.readlines()]
else:
filter_close_indices = []
for image_index in range(arguments.samples):
rgb = mpimg.imread(os.path.join(filter_path, arguments.filter_format.format(predicted_index)))
if np.median(rgb) > 5 and gt_index not in filter_close_indices:
indices.append((index, gt_index, predicted_index))
index += 1
gt_index += 1
predicted_index += 1
return indices
def read_file(filename, shape = None):
if filename.lower().endswith(".exr"):
depth_map = read_depth(filename)
return depth_map, depth_map < 1000.0
elif filename.lower().endswith(".png"):
depth_map = mpimg.imread(filename)
if shape is not None:
ih, iw = depth_map.shape
h, w = shape
if ih > 1024:
depth_map = depth_map[::2, ::2]
depth_map = zoom(depth_map, [float(h) / float(ih), w / float(iw)], order = 1)
mask = depth_map < 0.99
depth_map = depth_map * 65536 / 1000
return depth_map, mask
elif filename.lower().endswith(".npy"):
return np.load(filename), None
brain_tumor_segmentation_models.py 文件源码
项目:nn-segmentation-for-lar
作者: cvdlab
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def predict_image(self, test_img):
"""
predicts classes of input image
:param test_img: filepath to image to predict on
:return: segmented result
"""
# imgs = io.imread(test_img).astype('float').reshape(5, 216, 160)
imgs = mpimg.imread(test_img).astype('float')
imgs = rgb2gray(imgs).reshape(5, 216, 160)
plist = []
# create patches_to_predict from an entire slice
for img in imgs[:-1]:
if np.max(img) != 0:
img /= np.max(img)
p = extract_patches_2d(img, (33, 33))
plist.append(p)
patches_to_predict = np.array(
zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3])))
# predict classes of each pixel based on model
full_pred = self.model.predict_classes(patches_to_predict)
fp1 = full_pred.reshape(184, 128)
return fp1
def ret_val():
if os.path.isfile('./classifier_full.pickle'):
f = open('classifier_full.pickle', 'rb')
clf = pickle.load(f)
digit_loc = get_image_src2()
digit_image = mpimg.imread(digit_loc)
gray_digit = np.dot(digit_image[...,:3], [0.299, 0.587, 0.114])
digit_display = gray_digit
gray_digit = gray_digit.flatten()
for i in range(len(gray_digit)):
gray_digit[i] = 1.0 - gray_digit[i]
gray_digit[i] = round(gray_digit[i], 8)
return str(int(clf.predict([gray_digit])[0]))
def recolor_image(input_file, k=5):
img = mpimg.imread(path_to_png_file)
pixels = [pixel for row in img for pixel in row]
clusterer = KMeans(k)
clusterer.train(pixels) # this might take a while
def recolor(pixel):
cluster = clusterer.classify(pixel) # index of the closest cluster
return clusterer.means[cluster] # mean of the closest cluster
new_img = [[recolor(pixel) for pixel in row]
for row in img]
plt.imshow(new_img)
plt.axis('off')
plt.show()
#
# hierarchical clustering
#
project3.py 文件源码
项目:Self-Driving-Car-ND-Predict-Steering-Angle-with-CV
作者: sjamthe
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def fetch_image(i=0):
#reading in an image from test
images = ['test_images/solidWhiteCurve.jpg',
'test_images/solidWhiteRight.jpg',
'test_images/solidYellowCurve.jpg',
'test_images/solidYellowCurve2.jpg',
'test_images/solidYellowLeft.jpg',
'test_images/whiteCarLaneSwitch.jpg',
'test_images/challenge/test-14.jpg']
if(i > 6):
i = 6
if(i<0):
i=0
image = mpimg.imread(images[i])
return image
# In[120]:
def testClassifier():
clf = pickle.load(open("classifier.p", "rb"))
classes = numpy.loadtxt(dataset_root + 'classes.txt', dtype=str)
classes = column(classes, 0)
image_files = sorted(listdir(
join(project_root, test_images_dir)))
for image in image_files:
features = extractFeatures(
str(abspath(join(project_root, test_images_dir, image))))
prediction = clf.predict(features)
img = mpimg.imread(
str(abspath(join(project_root, test_images_dir, image))))
fig = plt.figure()
fig.suptitle(classes[int(prediction[0])],
fontsize=14, fontweight='bold')
plt.imshow(img)
def read_3channel_images(image_filename, num_images, file_regex):
images = []
for i in range(1, num_images + 1):
imageid = file_regex % i
filename = image_filename + imageid + ".png"
if os.path.isfile(filename):
print('Loading ' + filename)
img = mpimg.imread(filename)
tmp = np.array(img)
if len(tmp.shape) == 3:
img = img[:, :, :3]
images.append(img)
else:
print('File ' + filename + ' does not exist')
return np.array(images)
def tile_ims(filename, directory):
"""Load all images in the given directory and tile them into one."""
ims = [mpimg.imread(os.path.join(directory, f)) for f in sorted(os.listdir(directory))]
ims = np.array(ims)
ims = ims.transpose((0, 3, 1, 2)) # (n, h, w, c) -> (n, c, h ,w)
save_ims(filename, ims)
def read_from_file(self, filepathname, normalize = True):
# import image from file
# todo warnings about file existing
img = mpimg.imread(filepathname)
img = MyRGBImg(img)
if img.data.shape[2] == 4:
colors = []
for i in range(3):
channel = img.get_channel(i)
colors.append(channel)
# initializate the image
myimage = MyRGBImg(data = np.zeros((img.data.shape[0],
img.data.shape[1],
3)))
for i in range(3):
channel = colors[i]
channel.data = np.transpose(channel.data)
myimage.set_channel(channel, i)
self.data = myimage.data
else:
self.data = img.data
if normalize:
self.limit(1)
def read_from_file(self, filepathname):
''' import image from file using the mpimg utility of matplotlib'''
# todo warnings about file existing ?
self.data = mpimg.imread(filepathname)
def draw(self, figure):
"""
Draw watermark
Parameters
----------
figure : Matplotlib.figure.Figure
Matplolib figure on which to draw
"""
X = mimage.imread(self.filename)
figure.figimage(X, **self.kwargs)
def data_look(car_list, notcar_list):
data_dict = {}
# Define a key in data_dict "n_cars" and store the number of car images
data_dict["n_cars"] = len(car_list)
# Define a key "n_notcars" and store the number of notcar images
data_dict["n_notcars"] = len(notcar_list)
# Read in a test image, either car or notcar
test_img = mpimg.imread(car_list[0])
# Define a key "image_shape" and store the test image shape 3-tuple
data_dict["image_shape"] = test_img.shape
# Define a key "data_type" and store the data type of the test image.
data_dict["data_type"] = test_img.dtype
# Return data_dict
return data_dict