Python,Image processing , Morphology - python

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()

Related

ROI for image dataset

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.

VsCode matplotlib doesnt show figure window

import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("Desktop\sudoku.jpg", 0)
img = np.float32(img)
plt.figure()
plt.plot()
plt.imshow(img), plt.axis("off")
cv2.waitKey(0)
code doesn't give error but figure window is not opening
I think you are mixing cv2 and matplotlib. If you just want to display the image you can do it like so:
Using cv2:
import cv2
I = cv2.imread('path/to/img')
cv2.imshow('window_name', I)
cv2.waitKey(0)
or you can use matplotlib by simply adding plt.show() at the end as #JeruLuke pointed out. Just remember that OpenCV reads the image as BGR and this would make the colors show up weird when you display the image. You can use cv2.imread('path/to/image.jpg', COLOR_BGR2RGB) if you are set on using matplotlib.

How do i get the original resolution of the image like it was when i open it normally outside python

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.

How can I cut the axis for this image and center the axis?

I start to learn about segmentation, and I want to start with my picture, but when I attach my image, the axis are not where I want. Also, I try with a filter but it doesn't work for my picture.
this is the picture the code create:
import scipy.ndimage
from PIL import Image
from scipy.ndimage import gaussian_filter
im= Image.open('0.tiff')
#im.show()
plt.imshow(im)
im = scipy.ndimage.gaussian_filter(im, sigma=1)

rgb2gray not working, creating rainbow image

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

Categories