0

I'm trying to process image on OpenCV Python3. I searched and tried various methods to do the processing.

This is far as I could go, I need some help or tips or at least what should i look for.

This is what i use line detection for detecting outlines of carpet:

My sample input images are stored on the mega.nz site.

This is the code:

#pygame and camera lib
import os
import pygame, sys
from pygame.locals import *
import pygame.camera

import numpy as np
import cv2

def draw_lines(img, houghLines, color=[0, 255, 0], thickness=2):
    for line in houghLines:
        for rho,theta in line:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a*rho
            y0 = b*rho
            x1 = int(x0 + 1000*(-b))
            y1 = int(y0 + 1000*(a))
            x2 = int(x0 - 1000*(-b))
            y2 = int(y0 - 1000*(a))
 
            cv2.line(img,(x1,y1),(x2,y2),color,thickness)   
                
 
def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
    return cv2.addWeighted(initial_img, α, img, β, λ) 

#Variables
width = 1024
height = 768
Threshold1 = 10;
Threshold2 = 40;
FilterSize = 4
rho_resolution = 1
theta_resolution = np.pi/180
threshold = 155

#initialise pygame   
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()

#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')
i=1
while i<50 :
    #take a picture
    image = cam.get_image()
    #display the picture
    windowSurfaceObj.blit(image,(0,0))
    pygame.display.update()
    #pygame.image.save(windowSurfaceObj,'picture2.jpg')
    #image=cv2.imread('picture2.jpg',1)
    print(i,". frame")
    i=i+1;
    gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    blurred_image = cv2.GaussianBlur(gray_image, (25, 25), 0)
    
    
    edges_image = cv2.Canny(blurred_image, Threshold1, Threshold2, FilterSize)
       
    
    hough_lines = cv2.HoughLines(edges_image, rho_resolution , theta_resolution , threshold)
 
    hough_lines_image = np.zeros_like(image)
    draw_lines(hough_lines_image, hough_lines)
    original_image_with_hough_lines = weighted_img(hough_lines_image,image) 
    windowSurfaceObj.blit(image,(0,0))
    pygame.display.update()
    #imshow hough_lines_image & draw_lines & original_image_with_hough_lines
    
print("fin.")


pygame.display.quit()
cam.stop()

My problem is with this code: if I remove image processing just to observe, the image quality is kinda bad, but if I can process it, it might be OK for me.

  • If i save image and read it with cv2.imread() i get this error:

    windowSurfaceObj.blit(image,(0,0)) TypeError: argument 1 must be pygame.Surface, not numpy.ndarray

  • If i dont save&read it and just use image from this line image = cam.get_image(), OpenCV doesnt like it and says:

    TypeError: src is not a numpy array, neither a scalar

How can I convert these datatypes?

Is there any other way I can use for this process?

I want to process images like the ones on top and I use logitech c110 camera and Raspberry Pi 3 model B+. Could you, perhaps, suggest a more suitable way to approach this problem?

Community
  • 1
  • 1
  • `pygame.surfarray.make_surface` from the image that you want to display, and then `blit` the result. Search for that function name and you'll find plenty of examples. – Dan Mašek Sep 17 '18 at 21:11
  • Thank you so much Dan both for edit and answer, i checked it out i can now make_surface from numpy.array as follow up question can you suggest me a method for acquisition of images besides the one i use here pygame.camera i found out that there is video4linux lib but i only saw it saves images in buffer and processes it want to make it work continiously – Mustafa Yaşar Sep 18 '18 at 06:03
  • You can also convert surfaces to numpy arrays. [See this article](https://www.pygame.org/docs/tut/SurfarrayIntro.html) -- functions like `pixels3d` and `array3d` from the same module. | Since you're using OpenCV for the processing, you could use OpenCV's `VideoCapture` class to acquire images from a camera. – Dan Mašek Sep 18 '18 at 13:17
  • 1
    Thanks again Dan i managed to get it work with `VideoCapture`. At first it didnt work after some research i found out that i need to enable camera for opencv with `sudo modprobe bcm2835-v4l2` on terminal after that it worked just fine – Mustafa Yaşar Sep 19 '18 at 10:12

0 Answers0