Imwrite saves images as black .

asked 2020-03-24 04:48:50 -0600

kanankhan gravatar image

updated 2020-03-24 07:51:14 -0600

supra56 gravatar image

I am trying to write a program that finds center of black objects and cuts around that area. I applied it in 2 different data sets, in first it worked as expected but in second when I tried to save images it only saved full black images. here is my code:

 img = '/Users/khand/OneDrive/Desktop/Thesis/case_db_10/' + el 
                    print(type(img))
                    print(img)
                    im = cv2.imread(img).convert('RGBA')
                    print(type(im))
                    #plt.imshow(im)
                    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
                    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
                    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY_INV)[1]
                    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
                    cnts = imutils.grab_contours(cnts)
                    na = 0 
                    for c in range(0,len(cnts)):
                        cnt = cnts[c]
                        M = cv2.moments(cnt)
                        cx = int(M['m10']/M['m00'])
                        cy = int(M['m01']/M['m00'])
                        imag_jpg = '/Users/khand/OneDrive/Desktop/Thesis/case_db_10/' + nam +'.jpg'
                        imag_png = '/Users/khand/OneDrive/Desktop/Thesis/case_db_10/' + im_nam2[0] + '.png'
                        image_jpg = cv2.imread(imag_jpg)
                        image_png = cv2.imread(imag_png)
                        y1 = cx-100
                        y2 = cx+100
                        x1 = cy-100
                        x2 = cy+100
                        if x2 > 2000:
                            x2 = 2000
                            x1 = 1800
                        if y2 > 2000:
                            y2 = 2000 
                            y1 = 1800
                        if x1 < 0:
                            x1 = 0
                            x2 = 200
                        if y1 <0:
                            y1 = 0
                            y2 = 200

                        crop_imag_jpg = image_jpg[x1:x2, y1:y2].copy()
                        crop_imag_png = image_png[x1:x2, y1:y2].copy()
                        #ima_name_jpg = '/Users/khand/OneDrive/Desktop/Thesis/Case_db/org/cropped_jpg_' +str(nam_num)+'_'+str(im_nam2_num)+'_'+str(na)+'.jpg' 
                        #ima_name_png = '/Users/khand/OneDrive/Desktop/Thesis/Case_db/png/cropped_png_' +str(nam_num)+'_'+str(im_nam2_num)+'_'+str(na)+'.png'
                        ima_name_jpg = '/Users/khand/OneDrive/Desktop/Thesis/Case_db/org10/'+ nam +str(nam_num)+'_'+str(im_nam2_num)+'_'+str(na)+'.jpg' 
                        ima_name_png = '/Users/khand/OneDrive/Desktop/Thesis/Case_db/png10/'+ nam +str(nam_num)+'_'+str(im_nam2_num)+'_'+str(na)+'.png'

                        cv2.imwrite(ima_name_jpg,crop_imag_jpg)
                        cv2.imwrite(ima_name_png,crop_imag_png)

The difference in images are in their bit depth, in the working one it is 32 the one which is not working is 8. I am not sure if it is related to this or not. If you have any idea please feel free to help :) Thanks.! This one is working and 32 bit depth:

image description

This one is not working and 8 bit depth:

image description

edit retag flag offensive close merge delete

Comments

post original images(jpg, png)

supra56 gravatar imagesupra56 ( 2020-03-24 07:27:03 -0600 )edit
1

I edited original post and added images.

kanankhan gravatar imagekanankhan ( 2020-03-24 07:46:11 -0600 )edit

Can you rename extension png to jpg to see if you get 32 bit?

supra56 gravatar imagesupra56 ( 2020-03-24 07:56:44 -0600 )edit

Btw. Use variable name such as crop_imag_jpg1 and crop_imag_jpg2 and ima_name_jpg1 and ima_name_jpg2

supra56 gravatar imagesupra56 ( 2020-03-24 08:02:44 -0600 )edit

thanks I solved it simple adding 2 lines:

im = Image.open(img).convert('RGBA')
                    iimg = '/Users/khand/OneDrive/Desktop/Thesis/case_db_10_32bit/' + el + '.png'
                    # Save result
                    im.save(iimg)

like this, but now I have a different problem. When I read using cv2.imread it gives noneType, Could you please help in this case ?

kanankhan gravatar imagekanankhan ( 2020-03-24 08:11:25 -0600 )edit

I solved it as well but still it gives full black images

kanankhan gravatar imagekanankhan ( 2020-03-24 08:22:28 -0600 )edit

Did you change extension both jpg when using imwrite?

supra56 gravatar imagesupra56 ( 2020-03-24 09:02:43 -0600 )edit

Yeah I did but it didn't change the result.

kanankhan gravatar imagekanankhan ( 2020-03-24 09:08:00 -0600 )edit

It my lunch time.

supra56 gravatar imagesupra56 ( 2020-03-24 09:16:45 -0600 )edit