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')
This is our image processing homework, I know that we have to use morphological methods,and some cv2 methods like threshold , and we have to work on it as a colorful picture cause in Gray scale we will lose some information which we need.
We have to work on the attached photo input pic so we will have output photo this in output.
I'll appreciate any help
import cv2
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread('shapes.jpg')
img= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hist=cv2.calcHist(img,[0],None,[256],[0,256])
ret,img=cv2.threshold(img,110,255,cv2.THRESH_BINARY_INV)
#th=cv2.adaptiveThreshold(img,100,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,16,5)
se=np.uint8([[-1,0,1],[-1,0,1],[-1,0,1]])
se=np.ones(5*5)
se=cv2.getStructuringElement(cv2.MORPH_RECT,(15,5))
img=cv2.morphologyEx(img,cv2.MORPH_CLOSE,se)
img=cv2.erode(img,se)
#plt.plot(hist)
plt.gray()
plt.imshow(img)
plt.show()
I have changed the format of the images to png also..but of no use. Does cv2 / imshow decrease the resolution automatically?
import numpy as np
import cv2
from matplotlib import pyplot as plt
imgL = cv2.imread('image.png',0)
imgR = cv2.imread('2.png',0)
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgR,imgL)
plt.imshow(disparity, 'gray')
plt.show()
My main aim is to generate the final image with the resolution as was the supplied images.
You're using imshow from matplotlib which might be the cause of different showing behaviour.
Instead try:
cv2.imshow("Res", disparity)
cv2.waitKey(0)
cv2.destroyAllWindows()
If that is still not good, please edit the question and include the resulting image and the input image.
This is my code in python to read TIF and save it as png file.
import numpy as np
from PIL import Image
import matplotlib.image as mpimg
#im.show()
from skimage import io
import matplotlib.pyplot as plt
import matplotlib
import imageio
I = plt.imread('Shoes.TIF')
im= Image.fromarray(I)
print im.size
imageio.imsave('ShoesTest.png', I)
input_filename = "Shoes.TIF"
img =mpimg.imread(input_filename)
imgg= Image.fromarray(img)
print img
imgg.show()
However, when the new image is saved. I get an issue. When I try to check the resolution of the image in photoshop, I get this result.
Original : 240 pixels / inch
Saved Image : 96 pixels / inch
How do I save image in python that retains the resolution of the image? That is why I noticed visual changes in the images. Please help.