Javascript影像處理之將彩色圖轉換成灰階圖Javascript影像處理

來源:互聯網
上載者:User

  最近看了Justany_WhiteSnow的Javascript影像處理一文,寫的非常好,於是就練練手,略做了一些封裝:

(function () {    function imageToGray(iCanvas, url) {        this.canvas = iCanvas;        this.iCtx = this.canvas.getContext("2d");        this.url = url;    }    imageToGray.prototype = {        imread: function (_image) {            var width = _image.width,                height = _image.height;            this.iResize(width, height);            this.iCtx.drawImage(_image, 0, 0);            var imageData = this.iCtx.getImageData(0, 0, width, height),                tempMat = new Mat(height, width, imageData.data);            imageData = null;            this.iCtx.clearRect(0, 0, width, height);            return tempMat;        },        iResize: function (_width, _height) {            this.canvas.width = _width;            this.canvas.height = _height;        },        RGBA2ImageData: function (_imgMat) {            var width = _imgMat.col,                height = _imgMat.row,                imageData = this.iCtx.createImageData(width, height);            imageData.data.set(_imgMat.data);            return imageData;        },        render: function () {            var img = new Image();            var _this = this;            img.onload = function () {                var myMat = _this.imread(img);                var newImage = cvtColor(myMat);                var newIamgeData = _this.RGBA2ImageData(newImage);                _this.iCtx.putImageData(newIamgeData, 0, 0);            };            img.src = this.url;        }    };    function Mat(_row, _col, _data, _buffer){        this.row = _row || 0;        this.col = _col || 0;        this.channel = 4;        this.buffer = _buffer || new ArrayBuffer(_row * _col * 4);        this.data = new Uint8ClampedArray(this.buffer);        _data && this.data.set(_data);        this.bytes = 1;        this.type = "CV_RGBA";    }    function cvtColor(_src) {        if (_src.type && _src.type === "CV_RGBA") {            var row = _src.row,                col = _src.col;            var dst = new Mat(row, col);            data = dst.data,            data2 = _src.data;            var pix1, pix2, pix = _src.row * _src.col * 4;            while (pix) {                data[pix -= 4] = data[pix1 = pix + 1] = data[pix2 = pix + 2] = (data2[pix] * 299 + data2[pix1] * 587 + data2[pix2] * 114) / 1000;                data[pix + 3] = data2[pix + 3];            }        } else {            return src;        }        return dst;    }    window.imageToGray = imageToGray;})();

  調用方式如下:

var iCanvas = document.getElementById("grayImage");//canvas elementvar imgToGray = new imageToGray(iCanvas, "images/1.jpg");imgToGray.render();

  給一個完整的小例子吧:)

  本想給個線上運行地址的,但由於園子的圖片存在另一個網域名稱下,getImageData存在跨域安全問題,所以就給個吧(要放在本地的web service上運行哦,或直接丟在VS中運行也可以): 點此下載

  最後來張福利,哈哈!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.