Image conversion problem in python and OpenCV
Hi,
Below scrip work, but it is kind of stupid. I need to write and read file from HD in order to make it work. Is that possible to remove imwrite() and imread() from the middle of script. I think it is about image format issue, but I do not know how to fix it. Can any advanced expert show me a hint to fix this problem please? Thanks.
!/usr/bin/python
import cv2.cv as cv
import cv2
import numpy as np
img_8bit = cv2.imread("C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg",0)
clip_8bit=img_8bit[200:250,200:250]
# Below two lines write image and read it from HD.
cv2.imwrite("C:\Users\Sa\Pictures\clip_35.jpg",clip_8bit)
clip_8bit = cv2.imread("C:\Users\Sa\Pictures\clip_35.jpg")
#Is there a better way to convert file format and replace above two lines?
imgray = cv2.cvtColor(clip_8bit,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
BeadCount=hierarchy.shape[1]+1
you're writing/reading to disk to do a grayscale conversion ? what about :
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
It does not work. Is that possible something to do with "clip_8bit=img_8bit[200:250,200:250]"? After imwrite and imread, it is no longer an ROI. It is an complete image. I guess. If so, is there is a way to copy/clone an ROI image to a image varaiable. .
i don't quite understand you.
ofc, when you save a ROI of an image, it does not remember the original one.
but you want to get rid of the save/readback mechanism, don't you ?
Yes. I want to get rid of the save/readback mechanism. The original image is grayscale but within ROI. When I try to "findContours", it does not work. It work only after I use the save/readback mechanism. Is there a quick way to transfer image in ROI to a individual image?
Hi break. Thank you for your help. I found solution now. I was confused by the image's data type from example code. Thanks.