Python Forum
Why it gives me a black image and libpng warning: iCCP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why it gives me a black image and libpng warning: iCCP
#1
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from picamera2 import Picamera2
import cv2
import numpy as np
import time
import math
import argparse
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
PWM_pin   = 32   #Define pin, frequency and duty cycle
freq      = 50
dutyCycle = 7.5 #90 degrees at first#
                #Values 0 - 100 (represents 4%, ~27 deg)
minv=20
maxv=48
angles=[90]
width=1000
height=1333
rect1=(0,654
rect2=(1000,1005)
x1s=[]
x2s=[]
y1s=[]
y2s=[]
x3s=[]
y3s=[]
GPIO.setup(PWM_pin, GPIO.OUT) # Create PWM instance for pin w freqency
pwm = GPIO.PWM(PWM_pin, freq)
# picam2 = Picamera2()   
parser=argparse.ArgumentParser(description="lower and upper bound for trying")
parser.add_argument('--tim',default=2,help="length of time to run")
parser.add_argument('--delay',default=1,help="time between image captures")
parser.add_argument('--debug',action='store_true',default=True,help="debug mode")
args=parser.parse_args()
 
def get_duty(angle1,angle2):
    duty1=(1/18)*angle1 +2.2
    duty2=duty1+0.4*(angle2-angle1)*(1/18)
    return duty2
 
def get_points(img,debug):
     
    if img is None:
        print("Error: Unable to load image.")
        exit()
 
    height, width = (img.shape[:2])
     
    def blank(x):  # Null function for trackbar
        pass
     
     
    mask = np.zeros(img.shape[:2], dtype=np.uint8)
    mask = cv2.rectangle(mask,rect1,rect2, 255, -1)
    masked_img = cv2.bitwise_and(img, img, mask=mask)
     
    img=masked_img
    img =cv2.GaussianBlur(img, (3, 3), 0)
    edge = cv2.Canny(img, minv, maxv)
    lines = cv2.HoughLinesP(edge, 1, np.pi / 180, threshold=100, minLineLength=20, maxLineGap=10)
 
    output = img.copy()  # Avoid modifying the original image
    if lines is not None# Check if lines were detected
        for i in range(len(lines)):
            for x1, y1, x2, y2 in lines[i]:
                cv2.line(output, (x1, y1), (x2, y2), (0, 255, 0), 2)
                print(f'x1:{x1:0.2f}\ty1:{y1:0.2f}\tx2:{x2:0.2f}\ty2:{y2:0.2f}')
                x1s.append(x1)
                x2s.append(x1)
                y1s.append(y1)
                y2s.append(y2)
                with open('data.txt', 'a') as f:
                    f.write(f'{x1:0.2f}\t{y1:0.2f}\t{x2:0.2f}\t{y2:0.2f}')
                    f.write('\n')
 
    if debug== True:
        # cv2.imshow('image', img)
        # cv2.imshow('mask', mask)
        # cv2.imshow('masked_image', masked_img)
        cv2.imshow("houghline", output)
        cv2.waitKey(5000)
        cv2.destroyAllWindows() 
         
         
    # picam2.stop()
    return x1s,x2s,y1s,y2s
     
def get_angle(x1s,x2s,y1s,y2s):
    pass
     
     
     
     
     
start_time = time.time()
cur_time   = start_time
mesg_time  = start_time 
i=0
while (start_time + args.tim > cur_time):    
    time.sleep(0.001)            
    cur_time = time.time() # some short delay to avoid busy waits
    if (mesg_time + args.delay < cur_time):
        i+=1
        time_1=time.time()
        mesg_time = cur_time
        # picam2.start()
        img = cv2.imread("hallway.jpg") #take picture into memory
        height, width = img.shape[:2]
        scale = 0.5
        new_size  = (int(height*scale), int(width*scale))
        small_img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR)
        #先不缩小
        # img_RAM_BGR= cv2.cvtColor(img_RAM,cv2.COLOR_RGB2BGR) #change color
        #from RGB to BGR
        debug=args.debug
        # if debug == True:
            # cv2.imshow("picture",img_RAM_BGR)
            # cv2.waitKey(1000)
            # cv2.destroyAllWindows()
        # else:
            # pass
        # img_test = cv2.imread("bluetape.jpg")
        x1s,x2s,y1s,y2s=get_points(small_img,debug) #call function
        if debug == True:
            time_2=time.time()
            print(f'{time_2-time_1:0.2f}') #print the time it taken to complete the finding angle process
        angle=get_angle(x1s,x2s,y1s,y2s) #
        # dutyCycle=get_duty(angle1,angle2) #
        # pwm.ChangeDutyCycle(dutyCycle) #
        # if debug ==True:
            # print(f'PWM:{dutyCycle}')
        # else:
            # pass
Here is the error:
Quote:/home/ricky/Desktop/lab_deliverable/lab6/hallway.py:26: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(PWM_pin, GPIO.OUT) # Create PWM instance for pin w freqency
QStandardPaths: wrong permissions on runtime directory /run/user/1000, 0770 instead of 0700
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
6.66
0.26

the error happened when I added these lines:

1
2
3
scale = 0.5
new_size  = (int(height*scale), int(width*scale))
small_img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR)
Reply
#2
Problem Solve, I accidentally give the wrong coordinates for the resize function and the rectangle mask function
1
2
new_size  = (int(width*scale), int(height*scale))
    img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR)
Notice how that it is width in the first place and height in the second.

and

1
mask = cv2.rectangle(mask,rect1,rect2, 255, -1)
The rect1 and rect2 represents the upper-left and bottom-right coordinates and they should be updated accordingly since the image ins resized as 0.25 times big.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem When using canny edge detection,black image returned rickyw2777 1 352 Feb-17-2025, 03:22 AM
Last Post: rickyw2777
  Bright Black Screen Issue in Tkinter GUI Application rommy 2 801 Nov-29-2024, 10:50 PM
Last Post: woooee
  Black jack game simulation RoxaneParis1 3 1,206 Sep-11-2024, 06:57 AM
Last Post: indel635kanojia
  remove all color but red, then replace it with black kucingkembar 14 11,048 Dec-29-2021, 07:50 PM
Last Post: deanhystad
  Checkbuttons always come up as black boxes regardless of the state kenwatts275 5 6,503 Jul-07-2020, 08:00 PM
Last Post: kenwatts275
  How to use nb-black python cde formatter ErnestTBass 3 8,394 Jun-04-2020, 03:51 PM
Last Post: ErnestTBass
  Finance: Black Scholes Model not working pwt 5 5,228 May-27-2020, 10:14 AM
Last Post: buran
  Because the emoji appears black and white at the exit ? nerd 3 6,550 Jan-28-2019, 11:34 PM
Last Post: nerd
  Python interface only black and white........ Wilson 3 7,945 Jul-15-2017, 01:20 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020