Image data can't be converted to float - tesseract

#import required python modules
import cv2 # opencv
import pytesseract # tesseract
import matplotlib.pyplot as plt
import numpy as np
#read the text image using imread
org_img = cv2.imread("Poem.png")
plt.imshow(org_img) #display the read image
What is wrong with this code ??
I am getting the error that "Image data can't be converted into float"

Convert the image to RGB first:
org_img = cv2.imread("Poem.png")
org_img = cv2.cvtColor(org_img , cv2.COLOR_BGR2RGB)
plt.imshow(org_img)
plt.show()

This works for me in Python/OpenCV. You forgot to add plt.show() after plt.imshow(org_img)
#import required python modules
import cv2 # opencv
import pytesseract # tesseract
import matplotlib.pyplot as plt
import numpy as np
#read the text image using imread
org_img = cv2.imread("Poem.png")
plt.imshow(org_img) #display the read image
plt.show()

Related

Unable to visualise NetCDF DEM using basemap in jupyter

I am trying to visualise the DEM tiles which are in a netCDF format using python, but the following code is resulting in just the country boundaries and grids and not any data plotted.
from netCDF4 import Dataset
import netCDF4
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
nc = 'C:/Users/S05E135.SRTMGL1_NC.nc'
fh = Dataset(nc, mode='r')
fh.variables.keys()
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
height= fh.variables['SRTMGL1_DEM'][:]
height_units = fh.variables['SRTMGL1_DEM'].units
fh.close()
# Plotting the data
# Get some parameters for the Stereographic Projection
lon_0 = lons.mean()
lat_0 = lats.mean()
m = Basemap(width=4000000,height=3500000,
resolution='l',projection='stere',\
lat_ts=40,lat_0=lat_0,lon_0=lon_0)
# use meshgrid to create 2D arrays
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)
# Plot Data
cs = m.pcolor(xi,yi,np.squeeze(height))
# Add Grid Lines
m.drawparallels(np.arange(-80., 81., 10.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 10.), labels=[0,0,0,1], fontsize=10)
# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(height_units)
# Add Title
plt.title('dem')
plt.show()
Image of the output coming from the code

Imported GeoTIFF file returns only NAN values

I have been trying to import a GEOTIFF file that shows irradiance data. I am able to plot (see Plot here) the file so I am sure the file has data; however; the raster array contains only nan values. Is there any way to resolve this? I am quite new to python so perhaps I've missed something obvious, any help would be appreciated :)
This is the code I'm using:
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt
import gc
gc.enable()
#import
ds = gdal.Open('DNI_Spain.tif')
gt = ds.GetGeoTransform()
proj = ds.GetProjection()
band = ds.GetRasterBand(1)
array = band.ReadAsArray()
max_value = np.max(array)
min_value = np.min(array)
median_value = np.median(array)
plt.figure()
plt.imshow(array)

Image Color is not proper when converted to PIL

I am converting tensor to pil image, but color is distorted. I have tried converting it to BGR but result is still same.
trsn = transforms.ToPILImage()
temp = (target + torch.mean(target)) *torch.std(target)
res = trsn(temp.to('cpu').detach().squeeze())
np_img=np.array(res)
bgr = cv2.cvtColor(np_img,cv2.COLOR_RGB2BGR)
pil_im = Image.fromarray(bgr)
print(res)
# plt.imshow(res)
plt.imshow(res,label="Epoch "+str(i))
plt.show()
# im=Image.fromarray(np.uint8(target.to('cpu').detach().numpy()*255))
pil_im.save('f.jpeg')
I have a function that converts tensor to numpy and can show image on matplotlib.
Here is the function
def imcnvt(image):
x = image.to("cpu").clone().detach().numpy().squeeze()
x = x.transpose(1,2,0)
x = x*np.array((0.5,0.5,0.5)) + np.array((0.5,0.5,0.5))
return torch.Tensor(x)
Here is the pic I get
and here is the pic I want with PIL

Can I separate a Gif in each image is conformed?

I was wondering if there is any tool or if there is any way in order to take from a gif file, and save a file for each image.
This way I can get each frame separated in a file.
Using python
A script made in python is available here:
import os
from PIL import Image
def extractFrames(inGif, outFolder):
frame = Image.open(inGif)
nframes = 0
while frame:
frame.save( '%s/%s-%s.gif' % (outFolder, os.path.basename(inGif), nframes ) , 'GIF')
nframes += 1
try:
frame.seek( nframes )
except EOFError:
break;
return True
extractFrames('ban_ccccccccccc.gif', 'output')
ban_ccccccccccc.gif is the name of your GIF image, and output the name of the folder were the frames will be saved.

How to import pts file in Matlab

I have a pts file with 2d points (x,y) that looks like this:
version: 1
n_points: 5
{
159.128 108.541
230.854 109.176
164.841 179.633
193.404 193.597
192.769 229.143
}
How can I read this file and import this data into variables in Matlab?
Thanks.
i would do it like that
FileId=fopen(Filename)
npoints=textscan(FileId,'%s %f',1,'HeaderLines',1)
points=textscan(FileId,'%f %f',npoints{2},'MultipleDelimsAsOne',1,'Headerlines',1)
% now you have the values you want you can put them in a matrix or any variable
Y=cell2mat(C);