CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 06:51:13 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"c4df4ed625ab3456291afdb672f733f6"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com github.githubassets.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com marketplace-screenshots.githubusercontent.com/ copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=JUMVg5eD1%2FvrNXVQlUV4V6XxkY8RPakWOLk70OL8pnjHuFTN8yQ47xDtyU8Sco9rfptPguMNZK5VG0swvY6QA2n3YcCWoKe599GzmwskL%2BOiVYCXZlI4p0rICi%2BUKbIBHU2Yp9fzfcBeR2vV8bU8quLSdOTj8QHOq%2By7YQdO0kGq2ujbeLw3i%2FIenc0Kj1I1naK5IxA27P%2BzTtLRLnAN8mX%2FT9uhpZDw0qARuZ%2B3cUN%2BN5jOnWwH%2BjUx0HK2WSxZ2xmi20z4ezgVXaxcYmZ5Gg%3D%3D--%2Fet%2FdWca%2FCPoVuXp--6GaQHr0EU23OprgtQKQfYQ%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.101722356.1760165473; Path=/; Domain=github.com; Expires=Sun, 11 Oct 2026 06:51:13 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Sun, 11 Oct 2026 06:51:13 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: EC06:3B926C:3B8133:530FD0:68E9FE61
hough_by_hand.py · GitHub
Show Gist options
Save ilyakava/c2ef8aed4ad510ee3987 to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Last active
March 1, 2020 07:08
-
Star
4
(4)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save ilyakava/c2ef8aed4ad510ee3987 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: `python ~/Desktop/hough_by_hand.py 1994-654-12_v02.tif` | |
# output is a plot that pops up | |
import cv2 | |
import numpy as np | |
from matplotlib import pyplot as plt | |
import sys | |
import math | |
import pdb | |
# Loading image | |
if len(sys.argv) == 2: | |
filename = sys.argv[1] | |
else: | |
print "No input image given! \n" | |
img_orig = cv2.imread(filename,) | |
img = img_orig[:,:,::-1] # color channel plotting mess https://stackoverflow.com/a/15074748/2256243 | |
# https://nabinsharma.wordpress.com/2012/12/26/linear-hough-transform-using-python/ | |
def hough_transform(img_bin, theta_res=1, rho_res=1): | |
nR,nC = img_bin.shape | |
theta = np.linspace(-90.0, 0.0, np.ceil(90.0/theta_res) + 1.0) | |
theta = np.concatenate((theta, -theta[len(theta)-2::-1])) | |
D = np.sqrt((nR - 1)**2 + (nC - 1)**2) | |
q = np.ceil(D/rho_res) | |
nrho = 2*q + 1 | |
rho = np.linspace(-q*rho_res, q*rho_res, nrho) | |
H = np.zeros((len(rho), len(theta))) | |
for rowIdx in range(nR): | |
for colIdx in range(nC): | |
if img_bin[rowIdx, colIdx]: | |
for thIdx in range(len(theta)): | |
rhoVal = colIdx*np.cos(theta[thIdx]*np.pi/180.0) + \ | |
rowIdx*np.sin(theta[thIdx]*np.pi/180) | |
rhoIdx = np.nonzero(np.abs(rho-rhoVal) == np.min(np.abs(rho-rhoVal)))[0] | |
H[rhoIdx[0], thIdx] += 1 | |
return rho, theta, H | |
def top_n_rho_theta_pairs(ht_acc_matrix, n, rhos, thetas): | |
''' | |
@param hough transform accumulator matrix H (rho by theta) | |
@param n pairs of rho and thetas desired | |
@param ordered array of rhos represented by rows in H | |
@param ordered array of thetas represented by columns in H | |
@return top n rho theta pairs in H by accumulator value | |
@return x,y indexes in H of top n rho theta pairs | |
''' | |
flat = list(set(np.hstack(ht_acc_matrix))) | |
flat_sorted = sorted(flat, key = lambda n: -n) | |
coords_sorted = [(np.argwhere(ht_acc_matrix == acc_value)) for acc_value in flat_sorted[0:n]] | |
rho_theta = [] | |
x_y = [] | |
for coords_for_val_idx in range(0, len(coords_sorted), 1): | |
coords_for_val = coords_sorted[coords_for_val_idx] | |
for i in range(0, len(coords_for_val), 1): | |
n,m = coords_for_val[i] # n by m matrix | |
rho = rhos[n] | |
theta = thetas[m] | |
rho_theta.append([rho, theta]) | |
x_y.append([m, n]) # just to unnest and reorder coords_sorted | |
return [rho_theta[0:n], x_y] | |
def valid_point(pt, ymax, xmax): | |
''' | |
@return True/False if pt is with bounds for an xmax by ymax image | |
''' | |
x, y = pt | |
if x <= xmax and x >= 0 and y <= ymax and y >= 0: | |
return True | |
else: | |
return False | |
def round_tup(tup): | |
''' | |
@return closest integer for each number in a point for referencing | |
a particular pixel in an image | |
''' | |
x,y = [int(round(num)) for num in tup] | |
return (x,y) | |
def draw_rho_theta_pairs(target_im, pairs): | |
''' | |
@param opencv image | |
@param array of rho and theta pairs | |
Has the side-effect of drawing a line corresponding to a rho theta | |
pair on the image provided | |
''' | |
im_y_max, im_x_max, channels = np.shape(target_im) | |
for i in range(0, len(pairs), 1): | |
point = pairs[i] | |
rho = point[0] | |
theta = point[1] * np.pi / 180 # degrees to radians | |
# y = mx + b form | |
m = -np.cos(theta) / np.sin(theta) | |
b = rho / np.sin(theta) | |
# possible intersections on image edges | |
left = (0, b) | |
right = (im_x_max, im_x_max * m + b) | |
top = (-b / m, 0) | |
bottom = ((im_y_max - b) / m, im_y_max) | |
pts = [pt for pt in [left, right, top, bottom] if valid_point(pt, im_y_max, im_x_max)] | |
if len(pts) == 2: | |
cv2.line(target_im, round_tup(pts[0]), round_tup(pts[1]), (0,0,255), 1) | |
bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
edges = cv2.Canny(bw, threshold1 = 0, threshold2 = 50, apertureSize = 3) | |
rhos, thetas, H = hough_transform(edges) | |
rho_theta_pairs, x_y_pairs = top_n_rho_theta_pairs(H, 22, rhos, thetas) | |
im_w_lines = img.copy() | |
draw_rho_theta_pairs(im_w_lines, rho_theta_pairs) | |
# also going to draw circles in the accumulator matrix | |
for i in range(0, len(x_y_pairs), 1): | |
x, y = x_y_pairs[i] | |
cv2.circle(img = H, center = (x, y), radius = 12, color=(0,0,0), thickness = 1) | |
plt.subplot(141),plt.imshow(img) | |
plt.title('Original Image'), plt.xticks([]), plt.yticks([]) | |
plt.subplot(142),plt.imshow(edges,cmap = 'gray') | |
plt.title('Image Edges'), plt.xticks([]), plt.yticks([]) | |
plt.subplot(143),plt.imshow(H) | |
plt.title('Hough Transform Accumulator'), plt.xticks([]), plt.yticks([]) | |
plt.subplot(144),plt.imshow(im_w_lines) | |
plt.title('Detected Lines'), plt.xticks([]), plt.yticks([]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.