Python Forum
Saving a mask as a png file in opencv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving a mask as a png file in opencv
#1
Hi. I am trying to save as png file the mask generated by python when I run the object detection with HSV Color Space in opencv. I used cv2.imwriting function but it doesn't work. Is there another alternative to do this? I want to save the mask as I want later on to do pixel count on it based on colour. This is the code:

import cv2

import numpy as np

def nothing(x):
    pass

cv2.namedWindow("Tracking")

cv2.createTrackbar("LH", "Tracking", 0, 360, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 1, nothing)
cv2.createTrackbar("LV", "Tracking", 40, 40, nothing)
cv2.createTrackbar("UH", "Tracking", 360, 360, nothing)
cv2.createTrackbar("US", "Tracking", 0, 10, nothing)
cv2.createTrackbar("UV", "Tracking", 110, 110, nothing)

while True:
    frame = cv2.imread('cells.png')

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    l_h = cv2.getTrackbarPos("LH", "Tracking")
    l_s = cv2.getTrackbarPos("LS", "Tracking")
    l_v = cv2.getTrackbarPos("LV", "Tracking")

    u_h = cv2.getTrackbarPos("UH", "Tracking")
    u_s = cv2.getTrackbarPos("US", "Tracking")
    u_v = cv2.getTrackbarPos("UV", "Tracking")

    l_b = np.array([l_h, l_s, l_v])
    u_b = np.array([u_h, u_s, u_v])

    mask = cv2.inRange(hsv, l_b, u_b)

    res = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow("frame", frame)
    cv2.imshow("mask", mask)
    cv2.imshow("res", res)


    #cv2.imwrite(filename, mask1)
    key = cv2.waitKey(1)
    if key == 27:
        break

cv2.destroyAllWindows()
Thanks. Smile
Reply
#2
Why does the imwriting function not work? Are you getting an error message?
You could try and save it as a .jpg just for testing purpose. Do you need it in .png format or lets say image format in generel? If not you can use pickle to save the matrix, that represents the image :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving the times a script is run to a file or ... 3Pinter 7 1,411 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 820 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Saving the print result in a text file Calli 8 1,810 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Trying to determine attachment file type before saving off.. cubangt 1 2,165 Feb-23-2022, 07:45 PM
Last Post: cubangt
  How do I mask Hex value wiht 0xFF? korenron 2 4,486 Nov-23-2021, 09:13 AM
Last Post: Gribouillis
  Showing and saving the output of a python file run through bash Rim 3 2,475 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,318 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Need help with saving output into an excel file Beyondfacts 4 2,966 Mar-22-2021, 11:51 AM
Last Post: jefsummers
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,415 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  Using boolean mask in Numpy for 3D IlikePi 0 1,524 Nov-14-2020, 10:08 PM
Last Post: IlikePi

Forum Jump:

User Panel Messages

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