使用canvas html5为图像绘制线条轮廓

如何解决使用canvas html5为图像绘制线条轮廓

我正在尝试为图像绘制强度分布图,其中x轴为图像上线条的长度,y轴为强度值,沿着线条的长度。如何在html 5 canvas上执行此操作?我尝试了以下代码,但没有获得正确的强度值。不知道我要去哪里错了。

private getLineIntensityVals = function (lineObj,img) {
 const slope = this.calculateSlopeOfLine(lineObj.upPos,lineObj.downPos);
 const intercept = this.calculateIntercept(lineObj.downPos,slope);
 const ctx = img.getContext('2d');
 const coordinates = [];
 const intensities = [];
 for (let x = lineObj.downPos.x; x <= lineObj.upPos.x; x++) {
  const y = slope * x + intercept;
  const pixelData = ctx.getImageData(x,y,1,1).data;
  pixelData[0] = 255 - pixelData[0];
  pixelData[1] = 255 - pixelData[1];
  pixelData[2] = 255 - pixelData[2];
  const intensity = ((0.299 * pixelData[0]) + (0.587 * pixelData[1]) + (0.114 * pixelData[2]));
  intensities.push(intensity);
 }
 return intensities;
};

private calculateSlopeOfLine = function (upPos,downPos) {
 if (upPos.x === downPos.x || upPos.y === downPos.y) {
  return null;
 }
 return (downPos.y - upPos.y) / (downPos.x - upPos.x);
};

private calculateIntercept = function (startPoint,slope) {
  if (slope === null) {
   return startPoint.x;
  }
  return startPoint.y - slope * startPoint.x;
};

private calculateLineLength(line) {
  const dim = {width: Math.abs(line.downPos.x -line.upPos.x),height:Math.abs(line.downPos.y- line.upPos.y)};
  length = Math.sqrt(Math.pow(dim.width,2) + Math.pow(dim.height,2));
  return length;
};

解决方法

图像数据

不要一次获取一个像素的图像数据。获得对像素数据的访问成本很高(CPU周期),而内存则很便宜。一次获取所有像素,然后重复使用该数据。

数据采样

大多数线条不能均匀地放入像素中。要解决的问题是将行划分为所需的样本数(可以使用行长)

然后依次转到每个样本,获取4个相邻像素值并在样本点处插值颜色。

在进行内插时,我们需要确保不使用错误的颜色模型。在这种情况下,我们使用sRGB。

我们因此得到了功能

// imgData is the pixel date
// x1,y1 and x2,y2 are the line end points
// sampleRate is number of samples per pixel
// Return array 3 values for each sample.
function getProfile(imgData,x1,y1,x2,y2,sampleRate) {

    // convert line to vector
    const dx = x2 - x1;
    const dy = y2 - y1;

    // get length and calculate number of samples for sample rate
    const samples = (dx * dx + dy * dy) ** 0.5 * Math.abs(sampleRate) + 1 | 0;

    // Divide line vector by samples to get x,and y step per sample
    const nx = dx / samples;
    const ny = dy / samples;

    const w = imgData.width;
    const h = imgData.height;
    const pixels = imgData.data;
    const values = [];

    // Offset line to center of pixel
    var x = x1 + 0.5;
    var y = y1 + 0.5;
    var i = samples;

    while (i--) { // for each sample

        // make sure we are in the image
        if (x >= 0 && x < w - 1 && y >= 0 && y < h - 1) {

            // get 4 closest pixel indexes
            const idxA = ((x | 0) + (y | 0) * w) * 4;
            const idxB = ((x + 1 | 0) + (y | 0) * w) * 4;
            const idxC = ((x + 1 | 0) + (y + 1 | 0) * w) * 4;
            const idxD = ((x | 0) + (y + 1 | 0) * w) * 4;

            // Get channel data using sRGB approximation
            const r1 = pixels[idxA] ** 2.2;
            const r2 = pixels[idxB] ** 2.2;
            const r3 = pixels[idxC] ** 2.2;
            const r4 = pixels[idxD] ** 2.2;

            const g1 = pixels[idxA + 1] ** 2.2;
            const g2 = pixels[idxB + 1] ** 2.2;
            const g3 = pixels[idxC + 1] ** 2.2;
            const g4 = pixels[idxD + 1] ** 2.2;

            const b1 = pixels[idxA + 2] ** 2.2;
            const b2 = pixels[idxB + 2] ** 2.2;
            const b3 = pixels[idxC + 2] ** 2.2;
            const b4 = pixels[idxD + 2] ** 2.2;

            // find value at location via linear interpolation
            const xf = x % 1;
            const yf = y % 1;

            const rr = (r2 - r1) * xf + r1;
            const gg = (g2 - g1) * xf + g1;       
            const bb = (b2 - b1) * xf + b1;       

            /// store channels as uncompressed sRGB
            values.push((((r3 - r4) * xf + r4) - rr) * yf + rr);
            values.push((((g3 - g4) * xf + g4) - gg) * yf + gg);
            values.push((((b3 - b4) * xf + b4) - bb) * yf + bb);  
        } else {
            // outside image
            values.push(0,0);
        }
        // step to next sample
        x += nx;
        y += ny;
    }
    return values;
}

转换为值

该数组保存原始样本数据。有多种方法可以转换为值。这就是为什么我们将抽样从转换到值中分开。

下一个函数获取原始样本数组并将其转换为值。它返回一个值数组。在进行转换时,它还会获得最大值,以便可以绘制数据以适合图形。

function convertToMean(values) {
    var i = 0,v;
    const results = [];
    results._max = 0;
    while (i < values.length) {
        results.push(v = (values[i++] * 0.299 + values[i++] * 0.587 + values[i++] * 0.114) ** (1/2.2));
        results._max = Math.max(v,results._max);
    } 
    return results;
}

现在您可以按自己的喜好绘制数据了。

示例

点击图片上的拖动线(加载时)

结果是实时绘制的。

将鼠标移到绘图上可以查看值。

使用整页查看全部内容。

const ctx = canvas.getContext("2d");
const ctx1 = canvas1.getContext("2d");
const SCALE_IMAGE = 0.5;
const PLOT_WIDTH = 500;
const PLOT_HEIGHT = 150;
canvas1.width = PLOT_WIDTH;
canvas1.height = PLOT_HEIGHT;
const line = {x1: 0,y1: 0,x2: 0,y2:0,canUse: false,haveData: false,data: undefined};
var bounds,bounds1,imgData;
// ix iy image coords,px,py plot coords
const mouse  = {ix: 0,iy: 0,overImage: false,px: 0,py:0,overPlot: false,button : false,dragging: 0};
["down","up","move"].forEach(name => document.addEventListener("mouse" + name,mouseEvents));
const img = new Image;
img.crossOrigin = "Anonymous";
img.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Black_and_yellow_garden_spider%2C_Washington_DC.jpg/800px-Black_and_yellow_garden_spider%2C_Washington_DC.jpg";
img.addEventListener("load",() => {
    canvas.width = img.width;
    canvas.height = img.height;
    ctx.drawImage(img,0);
    imgData = ctx.getImageData(0,ctx.canvas.width,ctx.canvas.height);
    canvas.width = img.width * SCALE_IMAGE;
    canvas.height = img.height * SCALE_IMAGE;
    bounds = canvas.getBoundingClientRect();
    bounds1 = canvas1.getBoundingClientRect();
    requestAnimationFrame(update);
},{once: true});

function getProfile(imgData,sampleRate) {
    x1 *= 1 / SCALE_IMAGE;
    y1 *= 1 / SCALE_IMAGE;
    x2 *= 1 / SCALE_IMAGE;
    y2 *= 1 / SCALE_IMAGE;
    const dx = x2 - x1;
    const dy = y2 - y1;
    const samples = (dx * dx + dy * dy) ** 0.5 * Math.abs(sampleRate) + 1 | 0;
    const nx = dx / samples;
    const ny = dy / samples;
    const w = imgData.width;
    const h = imgData.height;
    const pixels = imgData.data;
    const values = [];
    var x = x1 + 0.5;
    var y = y1 + 0.5;
    var i = samples;
    while (i--) {
        if (x >= 0 && x < w - 1 && y >= 0 && y < h - 1) {
            // get 4 closest pixel indexs
            const idxA = ((x | 0) + (y | 0) * w) * 4;
            const idxB = ((x + 1 | 0) + (y | 0) * w) * 4;
            const idxC = ((x + 1 | 0) + (y + 1 | 0) * w) * 4;
            const idxD = ((x | 0) + (y + 1 | 0) * w) * 4;

            // Get channel data using sRGB approximation
            const r1 = pixels[idxA] ** 2.2;
            const r2 = pixels[idxB] ** 2.2;
            const r3 = pixels[idxC] ** 2.2;
            const r4 = pixels[idxD] ** 2.2;

            const g1 = pixels[idxA + 1] ** 2.2;
            const g2 = pixels[idxB + 1] ** 2.2;
            const g3 = pixels[idxC + 1] ** 2.2;
            const g4 = pixels[idxD + 1] ** 2.2;

            const b1 = pixels[idxA + 2] ** 2.2;
            const b2 = pixels[idxB + 2] ** 2.2;
            const b3 = pixels[idxC + 2] ** 2.2;
            const b4 = pixels[idxD + 2] ** 2.2;

            // find value at location via linear interpolation
            const xf = x % 1;
            const yf = y % 1;

            const rr = (r2 - r1) * xf + r1;
            const gg = (g2 - g1) * xf + g1;       
            const bb = (b2 - b1) * xf + b1;       

            /// store channels as uncompressed sRGB
            values.push((((r3 - r4) * xf + r4) - rr) * yf + rr);
            values.push((((g3 - g4) * xf + g4) - gg) * yf + gg);
            values.push((((b3 - b4) * xf + b4) - bb) * yf + bb);  
        } else {
            // outside image
            values.push(0,0);
        }
        x += nx;
        y += ny;
    }
    values._nx = nx;
    values._ny = ny;
    values._x = x1;
    values._y = y1;
    return values;
}
function convertToMean(values) {
    var i = 0,max = 0,v;
    const results = [];
    while (i < values.length) {
        results.push(v = (values[i++] * 0.299 + values[i++] * 0.587 + values[i++] * 0.114) ** (1/2.2));
        max = Math.max(v,max);
    }
    results._max = max;
    results._nx = values._nx;
    results._ny = values._ny;
    results._x = values._x;
    results._y = values._y;    
    return results;
}
function plotValues(ctx,values) {
    const count = values.length;
    const scaleX = ctx.canvas.width / count;
    // not using max in example
    // const scaleY = (ctx.canvas.height-3) / values._max;
    const scaleY = (ctx.canvas.height-3) / 255;
    ctx1.clearRect(0,ctx.canvas.height);
    var i = 0;
    ctx.beginPath();
    ctx.strokeStyle = "#000";
    ctx.lineWidth = 2;
    while (i < count) {
        const y = ctx.canvas.height - values[i] * scaleY + 1;
        ctx.lineTo(i++ * scaleX,y);
    }
    ctx.stroke();
    if (!mouse.button && mouse.overPlot) {
        ctx.fillStyle = "#f008";
        ctx.fillRect(mouse.px,1,ctx.canvas.height);
        const val = values[mouse.px / scaleX | 0];
        info.textContent = "Value: " + (val !== undefined ? val.toFixed(2) : "");
    }
}


function update() {
    ctx.clearRect(0,ctx.canvas.height);
    ctx.drawImage(img,img.width * SCALE_IMAGE,img.height * SCALE_IMAGE);
    var atSample = 0;
    if (!mouse.button) {
        if (line.canUse) {
            if (line.haveData && mouse.overPlot) {
                const count = line.data.length;
                const scaleX = ctx1.canvas.width / count            
                atSample = mouse.px / scaleX;
            }
        }
    }
    if (mouse.button) {
       if (mouse.dragging === 1) { // dragging line
          line.x2 = mouse.ix;
          line.y2 = mouse.iy;
          line.canUse = true;
          line.haveData = false;
       } else if(mouse.overImage) {
           mouse.dragging = 1;
           line.x1 = mouse.ix;
           line.y1 = mouse.iy;
           line.canUse = false;
           line.haveData = false;
           canvas.style.cursor = "none";
       }
    } else {
       mouse.dragging = 0;
       canvas.style.cursor = "crosshair";
    }
    if (line.canUse) {
        ctx.strokeStyle = "#F00";
        ctx.strokeWidth = 2;
        ctx.beginPath();
        ctx.lineTo(line.x1,line.y1);
        ctx.lineTo(line.x2,line.y2);
        ctx.stroke();
        if (atSample) {
            ctx.fillStyle = "#FF0";
            ctx.beginPath();
            ctx.arc(
                (line.data._x + line.data._nx * atSample) * SCALE_IMAGE,(line.data._y + line.data._ny * atSample) * SCALE_IMAGE,line.data[atSample | 0] / 32,Math.PI * 2
            );
            ctx.fill();
        
        }
        if (!line.haveData) {
            const vals = getProfile(imgData,line.x1,line.y1,line.x2,line.y2,1);
            line.data = convertToMean(vals);
            line.haveData = true;
            plotValues(ctx1,line.data);
        } else {
            plotValues(ctx1,line.data);
        }
    }
    requestAnimationFrame(update);
}

function mouseEvents(e){
    if (bounds) {
        mouse.ix = e.pageX - bounds.left;
        mouse.iy = e.pageY - bounds.top;
        mouse.overImage = mouse.ix >= 0 && mouse.ix < bounds.width && mouse.iy >= 0 && mouse.iy < bounds.height;
        mouse.px = e.pageX - bounds1.left;
        mouse.py = e.pageY - bounds1.top;
        mouse.overPlot = mouse.px >= 0 && mouse.px < bounds1.width && mouse.py >= 0 && mouse.py < bounds1.height; 
    }
    mouse.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
}
canvas {
    border: 2px solid black;
}    
<canvas id="canvas"></canvas>
<div id="info">Click drag line over image</div>
<canvas id="canvas1"></canvas>

图片来源:https://commons.wikimedia.org/w/index.php?curid=93680693,作者:BethGuay-自己的作品,CC BY-SA 4.0,

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-