I'm trying to find out ROI for an image dataset. I'm able to find ROI for a single image, but when it comes to the whole dataset or multiple images I can't do it.
I tried to use different code from the internet, but nothing worked.
`
import cv2
import numpy as np
import os
import random
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
img_folder=r'Shrimp/train/Resized_Dataset'
img_dir = r'Cropped_shrimp'
for i in range(30):
file = random.choice(os.listdir(img_folder))
image_path= os.path.join(img_folder, file)
img=mpimg.imread(image_path)
ax=plt.subplot(1,30,i+1)
ax.title.set_text(file)
plt.imshow(img)
roi = cv2.selectROI(img_folder)
ROI_image = img_folder[int(r[1]):int(r[1]+r[3]),
int(r[0]):int(r[0]+r[2])]
cv2.imshow("ROI_image", ROI_image)
cv2.waitKey(0)
os.chdir(img_dir)
cv2.imwrite("ROI_image", JPG)
`
This is my last approach. I think there're lots of mistake because I'm trying this for the first time.
I'm trying to create some pixelart using a matrix in pycharm. The problem is that I have never used this program. It's supposed to work just by simply selecting if you're working with the RGB model, but it doesn't.
import cv2
import numpy as np
from matplotlib import pyplot as plt
pixels = ([0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0])
([0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0])
([0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0])
([0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0])
print (pixels[2][4])
cv2.waitKey()
You need to save pixels as a numpy array with type uint8 and then let cv2 display it. If you pass 0 to waitKey the window will stay open until you close it manually.
import cv2
import numpy as np
pixels = np.array([[0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0],[0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0],[0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0],[0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0]], np.uint8)
cv2.imshow("My Image", pixels)
cv2.waitKey(0)
You can use the Pillow library.
from PIL import Image
import numpy as np
pixels = np.array(pixels).astype(np.uint8) # converts pixels to a numpy array
image = Image.fromarray(pixels)
# now you can save your image using
image.save('newimage.png')
I'm trying to download an svg image and open it in opencv for further processing. What I am doing is to convert from svg to png format with cairosvg, open it using Pillow and finally convert to opencv. The problem here is that the image converted in opencv does not look exactly the same as the one in PIL format.
from io import BytesIO
import numpy as np
import requests
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
# Get svg data
svg_data = requests.get(svg_url).content
# Convert from svg to png
png = svg2png(bytestring=svg_data)
# Open png in PIL
pil_img = Image.open(BytesIO(png))
pil_img.show() # This looks good
# Convert to opencv
cv_img = np.array(pil_img.convert('RGB'))[:, :, ::-1].copy() # Taken from https://stackoverflow.com/questions/14134892/convert-image-from-pil-to-opencv-format
cv2.imshow('cv_img', cv_img) # This does not look right
cv2.waitKey(0)
These are the resulting images from PIL and opencv format respectively:
The opencv image does not look right, e.g. the text does not have space between characters.
Why is that and how can I fix it?
As suggested, you have to preserve alpha channel.
import numpy as np
import requests
from io import BytesIO
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
svg_data = requests.get(svg_url).content
png = svg2png(bytestring=svg_data)
pil_img = Image.open(BytesIO(png)).convert('RGBA')
pil_img.save('output/pil.png')
cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGBA2BGRA)
cv2.imwrite('cv.png', cv_img)
I have performed a shear and horizona flip transformations on an float array saved in a PFM file, then I have saved them as tif files through this code:
import os
from scipy import ndarray
# image processing library
import skimage as sk
from skimage.transform import warp
from skimage import util
from skimage import io
import numpy as np
def shear(image_array: ndarray, shear=10):
tf_augment = sk.transform.AffineTransform(shear=np.deg2rad(shear))
return warp(image_array, tf_augment, order=1, preserve_range=True,
mode='edge')
def horizontal_flip(image_array: ndarray):
return image_array[:, ::-1]
image_path='./train/0006.pfm'
image_to_transform, scale=readPFM(image_path)
flipped_image1=horizontal_flip(image_to_transform)
io.imsave("flipped_image1.tif", flipped_image1)
sheared_image1 =shear(image_to_transform,5)
io.imsave("sheared_image1.tif", sheared_image1)
However, when I try to open these images with PIL framework, it works with the flipped image but not with sheared one:
from PIL import Image
Image.open("sheared_image1.tif")
I got the error
OSError: cannot identify image file 'sheared_image1.tif'
Could anyone explain to me the reason, or how to save my transformed images through skimage framework to be able to read them with PIL (I have to read them with PIL for latter purposes)
I'm trying to do data analysis on a series of photos, and when all photos are "changed" to grayscale from RGB, they are coming up as, well look:
The standard Astronaut image as my system says it's grayscale
Here's the code I'm using:
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray)
plt.show()
I also tried converting as hsv to rgb then grayscale, but it still produces a similar, non-grayscale image.
The problem is that matplotlib shows the image 2D with its default colormap. Change your code to
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray, cmap='gray')
plt.show()
You can also use
from skimage import io
io.imshow(img_gray)
which will handle grayscale images automatically