Q1.7 set Matrix zeroes

本文介绍了一种优化的算法,用于解决MxN矩阵中若元素为0,则将其所在行和列置为0的问题。通过使用哈希表减少内存占用,并优化了遍历过程,实现算法效率提升。

Q:Write an algorithm such that if an element in an MxN matrix is 0,its entire row and column is set to 0.

A: 通过遍历,可以用(m+n) 哈希表来储存每一列和每一行是否为0, 再根据哈希表的记录,再次遍历置0;

为了节省空间,可以在上面的思路基础之上进行优化:

1、首先遍历矩阵的第一行和第一列,记录下第一行和第一列是否有0

2、遍历矩阵,如果m[i][j] = 0, 则置m[i][0] = m[0][j] = 0;

3、再次遍历,如果m[i][0] == 0 或者 m[0][j] == 0, 则m[i][j] 置0

4、根据第一步的记录,决定是否将第一行和第一列置0还是置1

void setZeroes(vector<vector<int> > &matrix) {  
        bool row_has_zero = false;  
        bool col_has_zero = false;  
        const int m = matrix.size();  
        const int n = matrix[0].size();  
        for (int i = 0; i < m && !row_has_zero; i++) {  
            if (matrix[i][0] == 0) {  
                row_has_zero = true;  
            }  
        }  
        for (int j = 0; j < n && !col_has_zero; j++) {  
            if (matrix[0][j] == 0) {  
                col_has_zero = true;  
            }  
        }  
        for (int i = 1; i < m; i++) {  
            for (int j = 1; j < n; j++) {  
                if (matrix[i][j] == 0) {  
                    matrix[i][0] = 0;  
                    matrix[0][j] = 0;  
                }  
            }  
        }  
        for (int i = 1; i < m; i++) {  
            for (int j = 1; j < n; j++) {  
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {  
                    matrix[i][j] = 0;  
                }  
            }  
        }  
        if (row_has_zero) {  
            for (int i = 0; i < m; i++) {  
                matrix[i][0] = 0;  
            }  
        }  
        if (col_has_zero) {  
            for (int j = 0; j < n; j++) {  
                matrix[0][j] = 0;  
            }  
        }  
 }



``` import cv2 import torch import numpy as np class MonoDistance: def __init__(self, cam_matrix, person_height=1.7): self.cam_matrix = cam_matrix self.ref_height = person_height def estimate(self, bbox): pixel_height = bbox[3] - bbox[1] return (self.ref_height * self.cam_matrix[1,1]) / pixel_height def main(): # 加载模型 model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) # 相机参数 cam_matrix = np.array([[900, 0, 640], [0, 900, 360], [0, 0, 1]]) distance_estimator = MonoDistance(cam_matrix) cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) while True: ret, frame = cap.read() if not ret: break # 推理 results = model(frame) detections = results.pandas().xyxy[0] # 处理行人检测 for _, row in detections[detections['name'] == 'person'].iterrows(): x1, y1, x2, y2 = map(int, row[['xmin','ymin','xmax','ymax']]) distance = distance_estimator.estimate((x1,y1,x2,y2)) cv2.rectangle(frame, (x1,y1), (x2,y2), (0,255,0), 2) cv2.putText(frame, f"{distance:.2f}m", (x1,y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,255), 2) cv2.imshow('Demo', frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": main() import cv2 import torch import numpy as np class MonoDistance: def __init__(self, cam_matrix, dist_coeffs, person_height=1.7): self.cam_matrix = cam_matrix # 相机内参矩阵 self.dist_coeffs = dist_coeffs # 畸变系数 self.ref_height = person_height # 行人平均身高 def estimate(self, bbox): # 基于目标高度测距 y2 = bbox[3] y1 = bbox[1] pixel_height = abs(y2 - y1) return (self.ref_height * self.cam_matrix[1, 1]) / pixel_height def main(): # 加载改进模型 model = torch.hub.load('ultralytics/yolov5', 'custom', path='weights/best.pt') # 相机标定参数 cam_matrix = np.array([[900, 0, 640], [0, 900, 360], [0, 0, 1]]) dist_coeffs = np.array([-0.12, 0.25, 0, 0]) # 初始化测距模块 distance_estimator = MonoDistance(cam_matrix, dist_coeffs) # 摄像头初始化 cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) while True: ret, frame = cap.read() if not ret: break # YOLOv5检测 results = model(frame) detections = results.pandas().xyxy[0] # 处理每个检测目标 for _, row in detections[detections['name'] == 'person'].iterrows(): x1, y1, x2, y2 = map(int, row[['xmin', 'ymin', 'xmax', 'ymax']]) # 测距计算 distance = distance_estimator.estimate((x1, y1, x2, y2)) # 绘制结果 cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(frame, f"{distance:.2f}m", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2) # 显示帧率 cv2.putText(frame, f"FPS: {model.fps:.1f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow('Pedestrian Detection', frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": main()```修改上述代码,并详细指导用户如何把代码中yolov5模型,换成用户本地部署好的yolov5模型
03-12
function [A,b,x,theta,p,R,d] = fancurvedtomo(N,theta,p,R,d,isDisp,isMatrix) %FANCURVEDTOMO Creates 2D fan-beam curved-detector tomography test problem % % [A,b,x,theta,p,R,d] = fancurvedtomo(N) % [A,b,x,theta,p,R,d] = fancurvedtomo(N,theta) % [A,b,x,theta,p,R,d] = fancurvedtomo(N,theta,p) % [A,b,x,theta,p,R,d] = fancurvedtomo(N,theta,p,R) % [A,b,x,theta,p,R,d] = fancurvedtomo(N,theta,p,R,d) % [A,b,x,theta,p,R,d] = fancurvedtomo(N,theta,p,R,d,isDisp) % [A,b,x,theta,p,R,d] = fancurvedtomo(N,theta,p,R,d,isDisp,isMatrix) % % This function uses the "line model" to create a 2D X-ray tomography test % problem with an N-times-N pixel domain, using p rays in fan formation for % each angle in the vector theta. The detector is curved such that the angular % increments between rays in each projection is constant. % % Input: % N Scalar denoting the number of discretization intervals in % each dimesion, such that the domain consists of N^2 cells. % theta Vector containing the projetion angles in degrees. % Default: theta = 0:2:358. % p Number of rays for each angle. Default: p = round(sqrt(2)*N). % R The distance from the source to the center of the domain % is R*N. Default: R = 2. % d Scalar that determines the angular span of the rays, in % degrees. The default value is defined such that from the % source at (0,R*N) the first ray hits the corner (-N/2,N/2) % and the last ray hits the corner (N/2,N/2). % isDisp If isDisp is non-zero it specifies the time in seconds % to pause in the display of the rays. If zero (the default), % no display is shown. % isMatrix If non-zero, a sparse matrix is returned in A (default). % If zero, instead a function handle is returned. % % Output: % A If input isMatrix is 1 (default): coefficient matrix with % N^2 columns and length(theta)*p rows. % If input isMatrix is 0: A function handle representing a % matrix-free version of A in which the forward and backward % operations are computed as A(x,'notransp') and A(y,'transp'), % respectively, for column vectors x and y of appropriate size. % The size of A can be retrieved using A([],'size'). The matrix % is never formed explicitly, thus saving memory. % b Vector containing the rhs of the test problem. % x Vector containing the exact solution, with elements % between 0 and 1. % theta Vector containing the used angles in degrees. % p The number of used rays for each angle. % R The radius in side lengths. % d The span of the rays. % % See also: paralleltomo, fanlineartomo, seismictomo, seismicwavetomo, % sphericaltomo. % Reference: A. C. Kak and M. Slaney, Principles of Computerized % Tomographic Imaging, SIAM, Philadelphia, 2001. % Code written by: Per Christian Hansen, Jakob Sauer Jorgensen, and % Maria Saxild-Hansen, 2010-2017. % This file is part of the AIR Tools II package and is distributed under % the 3-Clause BSD License. A separate license file should be provided as % part of the package. % % Copyright 2017 Per Christian Hansen, Technical University of Denmark and % Jakob Sauer Jorgensen, University of Manchester. % Default illustration: if nargin < 6 || isempty(isDisp) isDisp = 0; end % Default value of R. if nargin < 4 || isempty(R) R = 2; end % Default value of d. if nargin < 5 || isempty(d) % Determine angular span. The first and last rays touch points % (-N/2,N/2) and (N/2,N/2), respectively. d = 2*atand(1/(2*R-1)); end % Default value of the number of rays p. if nargin < 3 || isempty(p) p = round(sqrt(2)*N); end % Default value of the angles theta. if nargin < 2 || isempty(theta) theta = 0:2:358; end % Make sure theta is double precison to prevent round-off issues caused by % single input. theta = double(theta); % Default matrix or matrix-free function handle? Matrix if nargin < 7 || isempty(isMatrix) isMatrix = 1; end % Input check. The source must lie outside the domain. if R < sqrt(2)/2 error('R must be greater than half squareroot 2') end R = R*N; % The width of the angle of the source. if d < 0 || d > 180 error('The angle of the source must be in the interval [0 180]') end % Construct either matrix or function handle. if isMatrix A = get_or_apply_system_matrix(N,theta,p,R,d,isDisp); else A = @(U,TRANSP_FLAG) get_or_apply_system_matrix(... N,theta,p,R,d,isDisp,U,TRANSP_FLAG); end if nargout > 1 % Create phantom head as a reshaped vector. x = phantomgallery('shepplogan',N); x = x(:); % Create rhs. if isMatrix b = A*x; else b = A(x,'notransp'); end end if nargout > 5 R = R/N; end function A = get_or_apply_system_matrix(N,theta,p,R,d,isDisp,u,transp_flag) % Anonymous function rotation matrix. Omega_x = @(omega_par) [cosd(omega_par) -sind(omega_par)]; Omega_y = @(omega_par) [sind(omega_par) cosd(omega_par)]; % nA denotes the number of angles. nA = length(theta); % The starting values both the x and the y coordinates. x0 = 0; y0 = R; xy0 = [x0;y0]; omega = linspace(-d/2,d/2,p); % The intersection lines. x = (-N/2:N/2)'; y = x; % Prepare for illustration. if isDisp AA = phantomgallery('smooth',N); figure end % Deduce whether to set up matrix or apply to input, deponding on whether % input u is given. isMatrix = (nargin < 7); if isMatrix % Initialize vectors that contains the row numbers, the column numbers % and the values for creating the matrix A effiecently. rows = zeros(2*N*nA*p,1); cols = rows; vals = rows; idxend = 0; II = 1:nA; JJ = 1:p; else % If transp_flag is 'size', only return size of operator, otherwise set % proper size of output. switch transp_flag case 'size' A = [p*nA,N^2]; return case 'notransp' % Forward project. if length(u) ~= N^2, error('Incorrect length of input vector'), end A = zeros(p*nA,1); case 'transp' % Backproject if length(u) ~= p*nA, error('Incorrect length of input vector'), end A = zeros(N^2,1); end % If u is a Cartesian unit vector then we only want to compute a single % row of A; otherwise we want to multiply with A or A'. if strcmpi(transp_flag,'transp') && nnz(u) == 1 && sum(u) == 1 % Want to compute a single row of A, stored as a column vector. ell = find(u==1); JJ = mod(ell,p); if JJ==0, JJ = p; end II = (ell-JJ)/p + 1; else % Want to multiply with A or A'. II = 1:nA; JJ = 1:p; end end % Loop over the chosen angles of the source. for i = II % The starting points for the current angle theta. x0theta = Omega_x(theta(i))*xy0; y0theta = Omega_y(theta(i))*xy0; % Illustration of the domain. if isDisp % illustration of source. clf pause(isDisp) imagesc((-N/2+.5):(N/2-0.5),(-N/2+.5):(N/2-0.5),flipud(AA)) colormap gray axis xy hold on axis(1.1*R*[-1,1,-1,1]) axis equal plot(x0theta,y0theta,'o','color',[60 179 113]/255,... 'linewidth',1.5,'markersize',10) end % The starting (center) direction vector (opposite xytheta) and % normalized. xytheta = [x0theta; y0theta]; abtheta = -xytheta/R; % Loop over the rays. for j = JJ % The direction vector for the current ray. a = Omega_x(omega(j))*abtheta; b = Omega_y(omega(j))*abtheta; % Illustration of rays. if isDisp plot([x0theta,x0theta+1.7*R*a],[y0theta,y0theta+1.7*R*b],'-',... 'color',[220 0 0]/255,'linewidth',1.5) axis(R*[-1,1,-1,1]) end % Use the parametrisation of line to get the y-coordinates of % intersections with x = constant. tx = (x - x0theta)/a; yx = b*tx + y0theta; % Use the parametrisation of line to get the x-coordinates of % intersections with y = constant. ty = (y - y0theta)/b; xy = a*ty + x0theta; % Collect the intersection times and coordinates. t = [tx; ty]; xxy = [x; xy]; yxy = [yx; y]; % Sort the coordinates according to intersection time. [~,I] = sort(t); xxy = xxy(I); yxy = yxy(I); % Skip the points outside the box. I = (xxy >= -N/2 & xxy <= N/2 & yxy >= -N/2 & yxy <= N/2); xxy = xxy(I); yxy = yxy(I); % Skip double points. I = (abs(diff(xxy)) <= 1e-10 & abs(diff(yxy)) <= 1e-10); xxy(I) = []; yxy(I) = []; % Illustration of the rays. if isDisp set(gca,'Xtick',[],'Ytick',[]) pause(isDisp) end % Calculate the length within cell and determines the number of % cells which is hit. aval = sqrt(diff(xxy).^2 + diff(yxy).^2); col = []; % Store the values inside the box. if numel(aval) > 0 % Calculates the midpoints of the line within the cells. xm = 0.5*(xxy(1:end-1)+xxy(2:end)) + N/2; ym = 0.5*(yxy(1:end-1)+yxy(2:end)) + N/2; % Translate the midpoint coordinates to index. col = floor(xm)*N + (N - floor(ym)); end if ~isempty(col) if isMatrix % Create the indices to store the values to vector for % later creation of A matrix. idxstart = idxend + 1; idxend = idxstart + numel(col) - 1; idx = idxstart:idxend; % Store row numbers, column numbers and values. rows(idx) = (i-1)*p + j; cols(idx) = col; vals(idx) = aval; else % If any nonzero elements, apply forward or back operator. if strcmp(transp_flag,'notransp') % Insert inner product with u into w. A( (i-1)*p+j ) = aval'*u(col); else % Adjoint operator. A(col) = A(col) + u( (i-1)*p+j )*aval; end end end end % end j end % end i if isMatrix % Truncate excess zeros. rows = rows(1:idxend); cols = cols(1:idxend); vals = vals(1:idxend); % Create sparse matrix A from the stored values. A = sparse(rows,cols,vals,p*nA,N^2); end能画一下这个的射线源坐标系旋转中心旋转方向嘛
最新发布
08-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值