Is there a way to guarantee a certain number of lines detected with cv2.HoughLines()?
This question is an extension to my previous question asking about how to detect a pool table's corners. I have found the outline of a pool table, and I have managed to apply the Hough transform on the outline. The result of this Hough transform is below:
Unfortunately, the Hough transform returns multiple lines for a single table edge. I want the Hough transform to return four lines, each corresponding to an edge of the table given any image of a pool table. I don't want to tweak the parameters for the Hough transform method manually (because the outline of the pool table might differ for each image of the pool table). Is there any way to guarantee four lines to be generated by cv2.HoughLines()?
Thanks in advance.
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
-
How To set the size of image more than default value in OpenCV
What is the default frame size of opencv image streaming frame ?
If I set Width and height of frame more than default frame size what happens?
-
How to read grayscale img from a video with OpenCV?
I read all pictures from my
pic
directory and then convert them each to gray-scale withcanny
edge detections before writing it all to a video.But, when I use my video software to play it, it shows a green background, and I can't read video frames from it. Could someone show me how to solve it?
Sample code
import numpy as np import cv2 as cv import matplotlib.pyplot as plt fourcc = cv.VideoWriter_fourcc(*"I420") out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 0) for pic in glob.glob1("./pic/", "A*"): img = cv.imread(f"./pic/{pic}", -1) edge = cv.Canny(img, 100, 200) edge = cv.resize(edge, (640, 480)) out.write(edge) out.release() # Cant read video frame here: cap = cv.VideoCapture("t2.avi") ret, frame = cap.read() if ret: plt.imshow(frame) else: print("end") cap.release()
-
Image Background Remover Using Python
I want to make Image Background Remover Using Python But I do not know how much data It will take and time to reach the accuracy of remove.bg I'm using U-2-Net Ai models https://github.com/xuebinqin/U-2-Net/ Some results are same but not every result is as good enough as remove.bg In a rating I would tell my app as 2/5 and remove.bg as 4/5 Please tell me How can I achieve accuracy like remove.bg Any help or suggestions are appreciated. Thanks
-
Android app data analysis over supermarket deals images
For an university project i need to create an Android application that collects and aggregates data from various supermarket flyers in order to filter deals by user inserted details.
As inspiration i am looking at commercial applications and i came across Shopfully that have a really interesting feature.
Their app is able to recognize the images of the flyers placing a floating plus button on each product.
Example:
If the user press on the plus floating button product details are showed (price, discount amount):
I am wondering how they made that? I imagine that they use some sort of image recognition to process flyers and an OCR text recognition to parse the text.
Would any of you have any ideas on how I might be able to reproduce this feature?
-
The edge module has not been pre-compiled for node.js version v14.18.1. You must build a custom version of edge.node. getting this error
{ "name": "electron-js", "version": "0.0.0", "description": "ElectronJs", "main": "app.js", "scripts": { "start": "electron ." }, "author": { "name": "Ralph" }, "dependencies": { "edge": "^7.10.1", "edge-js": "^16.6.0", "electron": "^1.6.2", "electron-edge": "6.5.5", "jquery": "3.2.1" }, "devDependencies": { "@types/electron": "1.4.34", "electron-packager": "^15.5.1", "electron-prebuilt": "^1.4.13" } }
here is my package.json file can you please tell me what's the issue in it, let me know if you need anything
-
why the hough transform can't find the line at right-below corner of the image?
I am trying use hough transform to find the edge, the following image's right-below corner line can't be found. why?
My code
BW = imread("edge/30.1.tif"); [H,theta,rho] = hough(BW); imshow(imadjust(rescale(H)),[],... 'XData',theta,... 'YData',rho,... 'InitialMagnification','fit'); xlabel('\theta (degrees)') ylabel('\rho') axis on axis normal hold on colormap(gca,hot); P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:)))); x = theta(P(:,2)); y = rho(P(:,1)); plot(x,y,'s','color','black'); lines = houghlines(BW,theta,rho,P,'FillGap',5,'MinLength',7); figure, imshow(BW), hold on max_len = 0; for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); % Plot beginnings and ends of lines plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red'); % Determine the endpoints of the longest line segment len = norm(lines(k).point1 - lines(k).point2); if ( len > max_len) max_len = len; xy_long = xy; end end % highlight the longest line segment plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
-
Find Corners of pool table for perspective warp with OpenCV
I have an image of a pool table with slight perspective distortion:
I am trying to un-warp the image (such that the corners of the pool table correspond to the corners of my output image) and then detect the pool balls on the output image. To unwarp the table, I intend to use
cv2.PerspectiveTransform()
to obtain the transformation matrix that maps the corners of the pool table to the corners of my output image. However, I do not have the locations of the corners of my table; I want to find the corners programmatically.So far, I have applied a mask (that filters out everything but the pool table). After applying the mask, I've applied a Canny edge detector to find the edges of the pool table only:
My idea was to use the Hough line transform algorithm to find the lines corresponding to the edges of the pool table (to find the corners of the pool table programmatically), but the Hough line transform algorithm seems to have a hard time detecting the edges well. How can I find the corners of the pool table from here?