{
 "app": "MathPad",
 "version": 3,
 "nextId": 99,
 "name": "图像处理示例.mpad",
 "regions": [
  {
   "id": 1,
   "x": 62,
   "y": 40,
   "kind": "text",
   "latex": "",
   "text": "图像处理示例（对齐 MATLAB 的写法）。两条规矩："
  },
  {
   "id": 2,
   "x": 62,
   "y": 74,
   "kind": "text",
   "latex": "",
   "text": "① imread 负责读入（返回的图像变量带尺寸和通道数：灰度 1 通道、彩色 3 通道）；\n② 整条管道里只有 imshow 会显示图像 —— 其它图像函数（imcrop / imthresh / imedge …）算出来的都是矩阵，不会自己蹦出一张图。想看哪个结果，就显式写 imshow(那个结果)。"
  },
  {
   "id": 3,
   "x": 62,
   "y": 147,
   "kind": "text",
   "latex": "",
   "text": "两张示例图随程序放在 samples/ 下，已经帮你载入：sample-gray.png（灰度，单通道，刻意做成\"左暗右亮\"的不均匀光照，用来对比全局阈值和自适应阈值）、sample-color.png（彩色，3 通道）。"
  },
  {
   "id": 4,
   "x": 62,
   "y": 200,
   "kind": "text",
   "latex": "",
   "text": "★ 所有图像函数都按原分辨率计算，谁都不会偷偷把图缩小（640×480 就是 640×480）。图太大算得慢时，用 imdown 显式降采样——那是唯一会缩小图像的函数（下面读完图再演示）。"
  },
  {
   "id": 5,
   "x": 78,
   "y": 253,
   "kind": "math",
   "latex": "gray:=imread(\\text{“sample-gray.png”})",
   "text": ""
  },
  {
   "id": 6,
   "x": 78,
   "y": 476,
   "kind": "math",
   "latex": "imshow\\left(gray\\right)",
   "text": ""
  },
  {
   "id": 7,
   "x": 62,
   "y": 971,
   "kind": "text",
   "latex": "",
   "text": "所以典型写法是「先赋值、再 imshow」："
  },
  {
   "id": 8,
   "x": 78,
   "y": 1005,
   "kind": "math",
   "latex": "g2:=imthresh\\left(gray,\\text{“otsu”}\\right)",
   "text": ""
  },
  {
   "id": 9,
   "x": 78,
   "y": 1110,
   "kind": "math",
   "latex": "imshow\\left(g2\\right)",
   "text": ""
  },
  {
   "id": 10,
   "x": 62,
   "y": 1605,
   "kind": "text",
   "latex": "",
   "text": "imdown(img, 0.5) 就是缩到一半："
  },
  {
   "id": 11,
   "x": 78,
   "y": 1639,
   "kind": "math",
   "latex": "imdown\\left(gray,0.5\\right)",
   "text": ""
  },
  {
   "id": 12,
   "x": 62,
   "y": 1953,
   "kind": "text",
   "latex": "",
   "text": "（0.5 = 缩到一半，这两张 640×480 的示例图就变成 320×240；写 160 这种大于 1 的数则当\"目标最大边长\"。缩完得到的是矩阵，照样可以 imshow / imthresh / imopen …）"
  },
  {
   "id": 13,
   "x": 78,
   "y": 2005,
   "kind": "math",
   "latex": "imshow\\left(imdown\\left(gray,0.5\\right)\\right)",
   "text": ""
  },
  {
   "id": 14,
   "x": 62,
   "y": 2319,
   "kind": "text",
   "latex": "",
   "text": "彩色图读进来就是 3 通道。imshow 的第二个参数可以只取某一个通道，取哪个通道，那个通道的颜色在灰度图里就是最亮的（纯色形状就是白的）："
  },
  {
   "id": 15,
   "x": 78,
   "y": 2371,
   "kind": "math",
   "latex": "color:=imread(\\text{“sample-color.png”})",
   "text": ""
  },
  {
   "id": 16,
   "x": 78,
   "y": 2421,
   "kind": "math",
   "latex": "imshow\\left(color\\right)",
   "text": ""
  },
  {
   "id": 17,
   "x": 62,
   "y": 2915,
   "kind": "text",
   "latex": "",
   "text": "红圆 → R 通道里是白的（红=255，绿蓝=0）："
  },
  {
   "id": 18,
   "x": 78,
   "y": 2948,
   "kind": "math",
   "latex": "imshow\\left(color,\\text{“r”}\\right)",
   "text": ""
  },
  {
   "id": 19,
   "x": 62,
   "y": 3442,
   "kind": "text",
   "latex": "",
   "text": "绿方 → G 通道里是白的："
  },
  {
   "id": 20,
   "x": 78,
   "y": 3475,
   "kind": "math",
   "latex": "imshow\\left(color,\\text{“g”}\\right)",
   "text": ""
  },
  {
   "id": 21,
   "x": 62,
   "y": 3969,
   "kind": "text",
   "latex": "",
   "text": "蓝三角 → B 通道里是白的："
  },
  {
   "id": 22,
   "x": 78,
   "y": 4002,
   "kind": "math",
   "latex": "imshow\\left(color,\\text{“b”}\\right)",
   "text": ""
  },
  {
   "id": 23,
   "x": 62,
   "y": 4496,
   "kind": "text",
   "latex": "",
   "text": "灰度（按亮度的加权平均）作对照 —— 红 0.30、绿 0.59、蓝 0.11，所以三个形状亮度不同："
  },
  {
   "id": 24,
   "x": 78,
   "y": 4529,
   "kind": "math",
   "latex": "imshow\\left(color,\\text{“gray”}\\right)",
   "text": ""
  },
  {
   "id": 25,
   "x": 62,
   "y": 5026,
   "kind": "text",
   "latex": "",
   "text": "点图右下角「图形选项」也能改通道（原样 / 灰度 / R / G / B），适配方式和插值也在那里：公式里写了通道就以公式为准，没写就听这里的设置。"
  },
  {
   "id": 26,
   "x": 62,
   "y": 5079,
   "kind": "text",
   "latex": "",
   "text": "要把某个通道当矩阵来算，用 imch 取出来（下面两种写法等价，都是 0–1 的矩阵，可以继续做阈值 / 边缘 / 形态学）："
  },
  {
   "id": 27,
   "x": 78,
   "y": 5113,
   "kind": "math",
   "latex": "red:=imch(color,\\text{“r”})",
   "text": ""
  },
  {
   "id": 28,
   "x": 78,
   "y": 5163,
   "kind": "math",
   "latex": "imshow\\left(red\\right)",
   "text": ""
  },
  {
   "id": 29,
   "x": 78,
   "y": 5658,
   "kind": "math",
   "latex": "imgstats\\left(red\\right)",
   "text": ""
  },
  {
   "id": 30,
   "x": 62,
   "y": 5709,
   "kind": "text",
   "latex": "",
   "text": "先看灰度图的统计：尺寸、均值、标准差，以及 Otsu 阈值。"
  },
  {
   "id": 31,
   "x": 78,
   "y": 5743,
   "kind": "math",
   "latex": "imgstats\\left(gray\\right)",
   "text": ""
  },
  {
   "id": 32,
   "x": 78,
   "y": 5794,
   "kind": "math",
   "latex": "graythresh\\left(gray\\right)=",
   "text": ""
  },
  {
   "id": 33,
   "x": 78,
   "y": 5844,
   "kind": "math",
   "latex": "imhist\\left(gray\\right)",
   "text": ""
  },
  {
   "id": 34,
   "x": 62,
   "y": 6198,
   "kind": "text",
   "latex": "",
   "text": "区间数（柱子根数）由你定：默认 256（MATLAB 也是 256），写第二个参数就行；也可以点图右上角 ⚙「图形选项 › 区间数」改。"
  },
  {
   "id": 35,
   "x": 78,
   "y": 6232,
   "kind": "math",
   "latex": "imhist\\left(gray,32\\right)",
   "text": ""
  },
  {
   "id": 36,
   "x": 62,
   "y": 6586,
   "kind": "text",
   "latex": "",
   "text": "二值化：同一条命令换方法就够了（在「函数」窗口的\"图像处理\"分类里也能找到）。"
  },
  {
   "id": 37,
   "x": 78,
   "y": 6620,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“otsu”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 38,
   "x": 78,
   "y": 7115,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“triangle”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 39,
   "x": 78,
   "y": 7610,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“isodata”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 40,
   "x": 78,
   "y": 8105,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“li”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 41,
   "x": 78,
   "y": 8600,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“yen”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 42,
   "x": 78,
   "y": 9095,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“moments”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 43,
   "x": 78,
   "y": 9590,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“minerror”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 44,
   "x": 78,
   "y": 10085,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“entropy”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 45,
   "x": 78,
   "y": 10580,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,0.5\\right)\\right)",
   "text": ""
  },
  {
   "id": 46,
   "x": 62,
   "y": 11075,
   "kind": "text",
   "latex": "",
   "text": "上面这些是\"整幅图一个阈值\"。光照不均匀时它们会把暗的一侧整片吃掉——"
  },
  {
   "id": 47,
   "x": 62,
   "y": 11109,
   "kind": "text",
   "latex": "",
   "text": "这时要用局部（自适应）阈值：每个像素拿自己邻域的统计量作阈值。"
  },
  {
   "id": 48,
   "x": 78,
   "y": 11143,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“adaptive”},31\\right)\\right)",
   "text": ""
  },
  {
   "id": 49,
   "x": 78,
   "y": 11638,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“gaussian”},31\\right)\\right)",
   "text": ""
  },
  {
   "id": 50,
   "x": 78,
   "y": 12133,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“sauvola”},31,0.2\\right)\\right)",
   "text": ""
  },
  {
   "id": 51,
   "x": 62,
   "y": 12628,
   "kind": "text",
   "latex": "",
   "text": "先去噪再二值化，通常更干净："
  },
  {
   "id": 52,
   "x": 78,
   "y": 12662,
   "kind": "math",
   "latex": "imshow\\left(immedfilt\\left(gray,3\\right)\\right)",
   "text": ""
  },
  {
   "id": 53,
   "x": 78,
   "y": 13157,
   "kind": "math",
   "latex": "imshow\\left(imgaussfilt\\left(gray,1.5\\right)\\right)",
   "text": ""
  },
  {
   "id": 54,
   "x": 78,
   "y": 13652,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(immedfilt\\left(gray,3\\right),\\text{“otsu”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 55,
   "x": 62,
   "y": 14147,
   "kind": "text",
   "latex": "",
   "text": "也可以整条链一行写完（里层先算，结果直接喂给外层）："
  },
  {
   "id": 56,
   "x": 78,
   "y": 14181,
   "kind": "math",
   "latex": "imshow\\left(imclose\\left(imthresh\\left(gray,\\text{“sauvola”},31,0.2\\right),3\\right)\\right)",
   "text": ""
  },
  {
   "id": 57,
   "x": 78,
   "y": 14700,
   "kind": "math",
   "latex": "imshow\\left(imthresh\\left(gray,\\text{“otsu”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 58,
   "x": 62,
   "y": 15195,
   "kind": "text",
   "latex": "",
   "text": "形态学：开运算去小亮点、闭运算填小暗点、填洞（右下那个方框会被填实）。"
  },
  {
   "id": 59,
   "x": 78,
   "y": 15229,
   "kind": "math",
   "latex": "b:=imthresh\\left(gray,\\text{“gaussian”},31\\right)",
   "text": ""
  },
  {
   "id": 60,
   "x": 78,
   "y": 15279,
   "kind": "math",
   "latex": "imshow\\left(imopen\\left(b,3\\right)\\right)",
   "text": ""
  },
  {
   "id": 61,
   "x": 78,
   "y": 15774,
   "kind": "math",
   "latex": "imshow\\left(imclose\\left(b,3\\right)\\right)",
   "text": ""
  },
  {
   "id": 62,
   "x": 78,
   "y": 16269,
   "kind": "math",
   "latex": "imshow\\left(imfill\\left(b\\right)\\right)",
   "text": ""
  },
  {
   "id": 63,
   "x": 78,
   "y": 16764,
   "kind": "math",
   "latex": "imshow\\left(imerode\\left(b,3\\right)\\right)",
   "text": ""
  },
  {
   "id": 64,
   "x": 78,
   "y": 17259,
   "kind": "math",
   "latex": "imshow\\left(imdilate\\left(b,3\\right)\\right)",
   "text": ""
  },
  {
   "id": 65,
   "x": 62,
   "y": 17754,
   "kind": "text",
   "latex": "",
   "text": "边缘检测可以换算子：sobel / prewitt / roberts / laplacian / log / canny。"
  },
  {
   "id": 66,
   "x": 78,
   "y": 17788,
   "kind": "math",
   "latex": "imshow\\left(imedge\\left(gray,\\text{“sobel”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 67,
   "x": 78,
   "y": 18283,
   "kind": "math",
   "latex": "imshow\\left(imedge\\left(gray,\\text{“roberts”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 68,
   "x": 78,
   "y": 18778,
   "kind": "math",
   "latex": "imshow\\left(imedge\\left(gray,\\text{“canny”}\\right)\\right)",
   "text": ""
  },
  {
   "id": 69,
   "x": 62,
   "y": 19273,
   "kind": "text",
   "latex": "",
   "text": "对比度与亮度：拉伸、自适应均衡（CLAHE）、Gamma。"
  },
  {
   "id": 70,
   "x": 78,
   "y": 19307,
   "kind": "math",
   "latex": "imshow\\left(imadjust\\left(gray\\right)\\right)",
   "text": ""
  },
  {
   "id": 71,
   "x": 78,
   "y": 19802,
   "kind": "math",
   "latex": "imshow\\left(adapthisteq\\left(gray,8\\right)\\right)",
   "text": ""
  },
  {
   "id": 72,
   "x": 78,
   "y": 20297,
   "kind": "math",
   "latex": "imshow\\left(imequalize\\left(gray\\right)\\right)",
   "text": ""
  },
  {
   "id": 73,
   "x": 78,
   "y": 20792,
   "kind": "math",
   "latex": "imshow\\left(imgamma\\left(gray,0.6\\right)\\right)",
   "text": ""
  },
  {
   "id": 74,
   "x": 62,
   "y": 21287,
   "kind": "text",
   "latex": "",
   "text": "几何变换：缩小、旋转、裁剪。"
  },
  {
   "id": 75,
   "x": 78,
   "y": 21321,
   "kind": "math",
   "latex": "imshow\\left(imresize\\left(gray,0.5\\right)\\right)",
   "text": ""
  },
  {
   "id": 76,
   "x": 78,
   "y": 21672,
   "kind": "math",
   "latex": "imshow\\left(imrotate\\left(gray\\right)\\right)",
   "text": ""
  },
  {
   "id": 77,
   "x": 62,
   "y": 22386,
   "kind": "text",
   "latex": "",
   "text": "裁剪也可以只赋值不显示，后面用的时候再 imshow："
  },
  {
   "id": 78,
   "x": 78,
   "y": 22419,
   "kind": "math",
   "latex": "gray3:=imcrop\\left(gray,20,40,60,180\\right)",
   "text": ""
  },
  {
   "id": 79,
   "x": 78,
   "y": 22469,
   "kind": "math",
   "latex": "imshow\\left(gray3\\right)",
   "text": ""
  },
  {
   "id": 80,
   "x": 62,
   "y": 22627,
   "kind": "text",
   "latex": "",
   "text": "裁剪 imcrop(m, v0, h0, H, W)：从左上角（行 v0、列 h0）裁出 H 高、W 宽的一块；H 或 W 写 0 就是一直到边缘（imcrop(gray,0,0,0,0) 等于原图）。"
  },
  {
   "id": 81,
   "x": 78,
   "y": 22680,
   "kind": "math",
   "latex": "imshow\\left(imcrop\\left(gray,60,200,96,160\\right)\\right)",
   "text": ""
  },
  {
   "id": 82,
   "x": 78,
   "y": 23021,
   "kind": "math",
   "latex": "imshow\\left(imcrop\\left(gray,0,0,0,0\\right)\\right)",
   "text": ""
  },
  {
   "id": 83,
   "x": 62,
   "y": 23516,
   "kind": "text",
   "latex": "",
   "text": "小技巧：点图右下角的「图形选项」可以改通道（原样/灰度/R/G/B）、适配方式和插值；拖图右下角能改大小。想把通道当矩阵参与运算就用 imch(color,\"r\")，要三通道一起拿去算就用 imread 出来的图像变量本身（它记着自己是 3 通道）。"
  },
  {
   "id": 84,
   "x": 78,
   "y": 24100,
   "kind": "text",
   "latex": "",
   "text": "八、彩色图 = 三维矩阵。imread 读进来的彩色图是「高 × 宽 × 3」的三维矩阵，裁剪、缩放、旋转、滤波这些运算都会保住三维（不会偷偷变成灰度）。下面把彩色图裁一块出来：c1 依旧是三维的，c1 = 会显示成矩阵，右上角能选显示多少行多少列，还能翻页看第 1/2/3 页（就是红/绿/蓝三个通道）。"
  },
  {
   "id": 85,
   "x": 78,
   "y": 24192,
   "kind": "math",
   "latex": "color2:=imread(\\text{“sample-color.png”})",
   "text": ""
  },
  {
   "id": 86,
   "x": 78,
   "y": 24258,
   "kind": "math",
   "latex": "c1:=imcrop(color2,100,100,120,160)",
   "text": ""
  },
  {
   "id": 87,
   "x": 78,
   "y": 24324,
   "kind": "math",
   "latex": "c1=",
   "text": ""
  },
  {
   "id": 88,
   "x": 78,
   "y": 24556,
   "kind": "text",
   "latex": "",
   "text": "imshow(c1) 默认按彩色显示（这就是\"彩色图别变灰\"的意思）。想看某一页（某个通道），直接把那一页取出来当矩阵用：c1_{[:,:,1]} 是第 1 页（红通道）、c1_{[:,:,2]} 绿、c1_{[:,:,3]} 蓝 —— 取出来是普通的二维矩阵，可以继续 imadjust / imthresh / imedge 这么往下算。"
  },
  {
   "id": 89,
   "x": 78,
   "y": 24608,
   "kind": "math",
   "latex": "imshow\\left(c1\\right)",
   "text": ""
  },
  {
   "id": 90,
   "x": 78,
   "y": 24817,
   "kind": "math",
   "latex": "c1_{[:,:,1]}=",
   "text": ""
  },
  {
   "id": 91,
   "x": 78,
   "y": 25049,
   "kind": "text",
   "latex": "",
   "text": "也能反过来赋值：c1_{[:,:,3]} := 0 就是把第 3 页（蓝通道）整页清成 0；c1_{[60,80,1]} := 1 是把第 1 页第 60 行第 80 列那一格点成最亮。:= 是赋值，右边不显示东西 —— 想确认就另起一行写 c1_{[60,80,1]} = 。"
  },
  {
   "id": 92,
   "x": 78,
   "y": 25101,
   "kind": "math",
   "latex": "c1_{[60,80,1]}:=1",
   "text": ""
  },
  {
   "id": 93,
   "x": 78,
   "y": 25152,
   "kind": "math",
   "latex": "c1_{[60,80,1]}=",
   "text": ""
  },
  {
   "id": 94,
   "x": 78,
   "y": 25203,
   "kind": "math",
   "latex": "imshow\\left(c1\\right)",
   "text": ""
  }
 ]
}
