lintcode:Rotate Image Show result

本文介绍了一种在不使用额外空间的情况下,将n x n的二维矩阵顺时针旋转90度的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise).

Example

Given a matrix

[
    [1,2],
    [3,4]
]

rotate it by 90 degrees (clockwise), return

[
    [3,1],
    [4,2]
]
Challenge

Do it in-place.

Tags   Expand  

Related Problems Expand 


class Solution {
public:
    /**
     * @param matrix: A list of lists of integers
     * @return: Void
     */
    void rotate(vector<vector<int> > &matrix) {
        // write your code here
        
        if (matrix.size() == 0 || matrix.size() == 1)
            return;
            
        int layer = matrix.size()/2;
        
        for (int layer=0; layer<matrix.size()/2; layer++)
        {
            int first = layer;
            int last  = matrix.size()-1-layer;
            
            for (int i=first; i<last; i++)
            {
                int offset = i-first;
                
                int top = matrix[first][i];
                
                matrix[first][i] = matrix[last-offset][first];
                matrix[last-offset][first] = matrix[last][last-offset];
                matrix[last][last-offset] = matrix[i][last];
                matrix[i][last] = top;
            }
        }
    }
};


import tkinter as tk from tkinter import filedialog, messagebox from PIL import Image, ImageTk import pytesseract # 配置Tesseract路径(Windows需要,根据实际情况修改) pytesseract.pytesseract.tesseract_cmd = r'D:\Tesseract\tesseract.exe' class OCRApp: def __init__(self, master): self.master = master master.title("OCR图像识别工具") # 创建UI组件 self.create_widgets() self.image_path = None def create_widgets(self): # 图片显示区域 self.image_label = tk.Label(self.master, borderwidth=2, relief="groove") self.image_label.pack(pady=10, padx=10, fill=tk.BOTH, expand=True) # 按钮区域 button_frame = tk.Frame(self.master) button_frame.pack(pady=5) # 上传按钮 self.upload_btn = tk.Button( button_frame, text="上传图片", command=self.upload_image, width=15 ) self.upload_btn.pack(side=tk.LEFT, padx=5) # 识别按钮 self.ocr_btn = tk.Button( button_frame, text="识别文字", command=self.perform_ocr, width=15 ) self.ocr_btn.pack(side=tk.LEFT, padx=5) # 结果展示区域 self.result_text = tk.Text( self.master, height=10, wrap=tk.WORD, font=("Arial", 10) ) self.result_text.pack(pady=10, padx=10, fill=tk.BOTH, expand=True) def upload_image(self): file_path = filedialog.askopenfilename( filetypes=[("图片文件", "*.png;*.jpg;*.jpeg;*.bmp")] ) if file_path: self.image_path = file_path self.show_image(file_path) def show_image(self, path): try: image = Image.open(path) # 调整图片尺寸以适应界面 max_size = (800, 600) image.thumbnail(max_size) photo = ImageTk.PhotoImage(image) self.image_label.config(image=photo) self.image_label.image = photo # 保持引用 except Exception as e: messagebox.showerror("错误", f"加载图片失败: {str(e)}") def perform_ocr(self): if not self.image_path: messagebox.showwarning("警告", "请先上传图片") return try: # 使用PIL打开图片 image = Image.open(self.image_path) # 进行OCR识别 text = pytesseract.image_to_string( image, lang='eng', # 使用英文语言包 config='--psm 6' # 识别单行文本 ) # 显示结果 self.result_text.delete(1.0, tk.END) self.result_text.insert(tk.END, text) except Exception as e: messagebox.showerror("识别错误", f"OCR处理失败: {str(e)}") if __name__ == "__main__": root = tk.Tk() app = OCRApp(root) root.geometry("800x600") root.mainloop() 问题:为什么识别成功概率不高?返回给我正确的完整的代码
最新发布
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值