Ask Your Question
0

Image conversion problem in python and OpenCV

asked 2014-04-03 02:10:20 -0600

syang2100 gravatar image

updated 2014-04-03 02:34:59 -0600

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
edit retag flag offensive close merge delete

Comments

2

you're writing/reading to disk to do a grayscale conversion ? what about :

img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

berak gravatar imageberak ( 2014-04-03 02:41:39 -0600 )edit

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

syang2100 gravatar imagesyang2100 ( 2014-04-04 01:33:36 -0600 )edit

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 ?

berak gravatar imageberak ( 2014-04-04 01:37:14 -0600 )edit

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?

syang2100 gravatar imagesyang2100 ( 2014-04-06 02:31:33 -0600 )edit

Hi break. Thank you for your help. I found solution now. I was confused by the image's data type from example code. Thanks.

syang2100 gravatar imagesyang2100 ( 2014-04-06 16:33:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-04-06 16:30:52 -0600

syang2100 gravatar image

Hi All,

After days struggle, I found the solution for this problem. Just remove (1), (2), (3), and add (4), shown below. I have data type conflict with the example provided in the internet. Now, the problem is resolved. Thank you for your help.

cv2.imwrite("C:\Users\Sa\Pictures\clip_35.jpg",clip_8bit) #(1), write as gray scale image

clip_8bit = cv2.imread("C:\Users\Sa\Pictures\clip_35.jpg") #(2), read as color image

imgray = cv2.cvtColor(clip_8bit,cv2.COLOR_BGR2GRAY) #(3), transfer to gray scale again

imgray = clip_8bit #(4)

edit flag offensive delete link more

Comments

Do accept your own solution if it fixed everything!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-22 06:17:53 -0600 )edit

Question Tools

Stats

Asked: 2014-04-03 02:10:20 -0600

Seen: 3,090 times

Last updated: Apr 21 '14