Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 file 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 line write image and read it from HD.  Is there a better way to convert file format?  Thanks.
cv2.imwrite("C:\Users\Sa\Pictures\clip_35.jpg",clip_8bit) 

clip_8bit = cv2.imread("C:\Users\Sa\Pictures\clip_35.jpg")### read from HD again

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

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 file 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 line lines write image and read it from HD.  Is 
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?  Thanks.
cv2.imwrite("C:\Users\Sa\Pictures\clip_35.jpg",clip_8bit) 

clip_8bit = cv2.imread("C:\Users\Sa\Pictures\clip_35.jpg")### read from HD again
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

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 file 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