この記事では、画像テキストセグメンテーションを実装するためのNodeJSの具体的なコードを参考までに紹介します。具体的な内容は次のとおりです。 var fs = require('fs'); var jpeg = require('jpeg-js') 関数 getSumRGB(データ, i) { var cr = データ.データ[i+0] var cg = データ.データ[i+1] var cb = データ.データ[i+2] var srgb = (cr+cg+cb) srgbを返す } 関数 getTopRGB(データ, i) { var topIndex = (データ幅 * 4 * -1) i = i + トップインデックス; var cr = データ.データ[i+0] var cg = データ.データ[i+1] var cb = データ.データ[i+2] [cr,cg,cb]を返す } 関数 getHeightRGB(データ, i, haveRGB) { var 幅 = データ.幅 var 高さ = data.height var len = 幅 * 高さ * 4 var 持っている数 = 0 (i=i;i<len;i+=width*4) の場合 { getSumRGB(データ、i) == haveRGBの場合 { 数値++を持つ } } haveNumを返す } 関数 ClearBackGround(データ) { var 幅 = データ.幅 var 高さ = data.height var len = 幅 * 高さ * 4 変数 i, tmp (i=0;i<len;i+=4) の場合 { tmp = getSumRGB(データ、i) tmp > 120*3の場合{ データ.データ[i+0]=255 データ.データ[i+1]=255 データ.データ[i+2]=255 } } (i=0;i<len;i+=4) の場合 { tmp = getSumRGB(データ、i) tmp <= 32*3の場合{ tmp = getTopRGB(データ、i) データ.data[i+0]=tmp[0] データ.データ[i+1]=tmp[1] データ.データ[i+2]=tmp[2] } } (i=0;i<len;i+=4) の場合 { tmp = getSumRGB(データ、i) tmp != 255*3 の場合 データ.データ[i+0]=0 データ.データ[i+1]=0 データ.データ[i+2]=0 } } var 最大幅 = 幅 * 4 var 引数リスト = [] 引数リスト[0]=[] 引数リスト[1]=[] 引数リスト[2]=[] 引数リスト[3]=[] 配列数 = 0 ブロック = 0 (i=0;i<最大幅;i+=4)の場合 { tmp = getHeightRGB(データ, i, 0) tmp==0の場合{ if(ブロック != 0) { arrlist[arrnum] = [ブロック/4,i/4] ブロック=0 arnum++ 引数 } } そうでない場合(tmp >0) { if(ブロック == 0) { ブロック = i } } } console.log(arrlist) データを返す } var picname = "tmp.jpg" var 新しい画像名 = "000.jpg" var jpegData = fs.readFileSync(picname) var rawImageData = jpeg.decode(jpegData, {useTArray: true}) rawImageData = ClearBackGround(rawImageData) var jpegImageData = jpeg.encode(rawImageData,100) fs.writeFileSync(新しい画像名、jpegImageData.data) JS で画像の切り取りを実装する方法を紹介します。 //画像の切り取り var ImgCropper = Class.create(); ImgCropper.プロトタイプ = { //コンテナオブジェクト、コントロールレイヤー、イメージアドレス初期化: function(container, handle, url, options) { this._Container = $(container);//コンテナ オブジェクトthis._layHandle = $(handle);//コントロール レイヤーthis.Url = url;//画像アドレスthis._layBase = this._Container.appendChild(document.createElement("img"));//下部レイヤーthis._layCropper = this._Container.appendChild(document.createElement("img"));//カット レイヤーthis._layCropper.onload = Bind(this, this.SetPos); //サイズを設定するために使用します this._tempImg = document.createElement("img"); this._tempImg.onload = Bind(this, this.SetSize); オプションを設定します。 this.Opacity = Math.round(this.options.Opacity); this.Color = this.options.Color; this.Scale = !!this.options.Scale; this.Ratio = Math.max(this.options.Ratio, 0); this.Width = Math.round(this.options.Width); this.Height = Math.round(this.options.Height); //プレビューオブジェクトを設定する var oPreview = $(this.options.Preview); //プレビューオブジェクト if (oPreview) { oPreview.style.position = "相対的"; oPreview.style.overflow = "非表示"; this.viewWidth = Math.round(this.options.viewWidth); this.viewHeight = Math.round(this.options.viewHeight); // プレビュー画像オブジェクト this._view = oPreview.appendChild(document.createElement("img")); this._view.style.position = "絶対"; this._view.onload = Bind(this, this.SetPreview); } //ドラッグ アンド ドロップを設定します。 this._drag = new Drag(this._layHandle, { Limit: true, onMove: Bind(this, this.SetPos), Transparent: true }); //ズームを設定 this.Resize = !!this.options.Resize; if(this.Resize){ var op = this.options、_resize = new Resize(this._layHandle、{Max: true、onResize: Bind(this、this.SetPos) }); //ズームトリガーオブジェクトを設定します op.RightDown && (_resize.Set(op.RightDown, "right-down")); op.LeftDown && (_resize.Set(op.LeftDown, "左下")); op.RightUp && (_resize.Set(op.RightUp, "右上")); op.LeftUp && (_resize.Set(op.LeftUp, "左上")); op.Right && (_resize.Set(op.Right, "right")); op.Left && (_resize.Set(op.Left, "left")); op.Down && (_resize.Set(op.Down, "down")); op.Up && (_resize.Set(op.Up, "up")); // 最小範囲制限 this.Min = !!this.options.Min; this.minWidth = Math.round(this.options.minWidth); this.minHeight = Math.round(this.options.minHeight); //スケーリング オブジェクトを設定します this._resize = _resize; } //スタイルを設定します this._Container.style.position = "relative"; this._Container.style.overflow = "非表示"; this._layHandle.style.zIndex = 200; this._layCropper.style.zIndex = 100; this._layBase.style.position = this._layCropper.style.position = "絶対"; this._layBase.style.top = this._layBase.style.left = this._layCropper.style.top = this._layCropper.style.left = 0; //配置 //初期化設定 this.Init(); }, //デフォルトのプロパティを設定する SetOptions: function(options) { this.options ={//デフォルト値不透明度: 50、//透明度 (0 ~ 100) Color: "", //背景色Width: 0, //画像の高さHeight: 0, //画像の高さ//スケーリング トリガー objectResize: false, //スケーリングを設定するかどうかRight: "", //右スケーリング objectLeft: "", //左スケーリング objectUp: "", //上部スケーリング objectDown: "", //下部スケーリング objectRightDown: "", //右下スケーリング objectLeftDown: "", //左下スケーリング objectRightUp: "", //右上スケーリング objectLeftUp: "", //左上スケーリング objectMin: false, //幅と高さの最小制限を設定するかどうか (以下の min パラメーターは true の場合に便利です) minWidth: 50, // 最小幅 minHeight: 50, // 最小高さ Scale: false, // 比例して拡大縮小するかどうか Ratio: 0, // 拡大縮小率 (幅/高さ) //プレビューオブジェクト設定 Preview: "", //プレビューオブジェクト viewWidth: 0, //プレビューの幅 viewHeight: 0 //プレビューの高さ }; Extend(this.options、オプション || {}); }, // オブジェクトを初期化する Init: function() { //背景色を設定します this.Color && (this._Container.style.backgroundColor = this.Color); //画像を設定します this._tempImg.src = this._layBase.src = this._layCropper.src = this.Url; //透明に設定する if(isIE){ this._layBase.style.filter = "alpha(不透明度:" + this.Opacity + ")"; } それ以外 { this._layBase.style.opacity = this.Opacity / 100; } //プレビュー オブジェクトを設定します this._view && (this._view.src = this.Url); //ズームを設定する if (this.Resize) { (this._resize){で スケール = this.Scale; 比率 = this.Ratio; 最小 = this.Min; 最小幅 = this.minWidth; 最小高さ = this.minHeight; } } }, //カットスタイルを設定する SetPos: function() { //IE6 レンダリング バグ if (isIE6) { with (this._layHandle.style) { zoom = .9; zoom = 1; }; }; //位置パラメータを取得します var p = this.GetPos(); //ドラッグ アンド ドロップ オブジェクトのパラメータに従ってカットします。this._layCropper.style.clip = "rect(" + p.Top + "px " + (p.Left + p.Width) + "px " + (p.Top + p.Height) + "px " + p.Left + "px)"; //プレビューを設定します this.SetPreview(); }, //プレビュー効果を設定する SetPreview: function() { if(this._view){ //プレビュー表示の幅と高さ var p = this.GetPos(), s = this.GetSize(p.Width, p.Height, this.viewWidth, this.viewHeight), scale = s.Height / p.Height; //パラメータを比例的に設定します var pHeight = this._layBase.height * scale, pWidth = this._layBase.width * scale, pTop = p.Top * scale, pLeft = p.Left * scale; //プレビューオブジェクトを(this._view.style){で設定します //スタイルを設定します。幅 = pWidth + "px"; 高さ = pHeight + "px"; 上 = - pTop + "px "; 左 = - pLeft + "px"; //プレビュー画像の切り取り clip = "rect(" + pTop + "px " + (pLeft + s.Width) + "px " + (pTop + s.Height) + "px " + pLeft + "px)"; } } }, //画像サイズを設定する SetSize: function() { var s = this.GetSize(this._tempImg.width、this._tempImg.height、this.Width、this.Height); // ベースマップとカッティングマップを設定します。this._layBase.style.width = this._layCropper.style.width = s.Width + "px"; this._layBase.style.height = this._layCropper.style.height = s.Height + "px"; //ドラッグアンドドロップ範囲を設定します。this._drag.mxRight = s.Width; this._drag.mxBottom = s.Height; //ズーム範囲を設定します if (this.Resize) { this._resize.mxRight = s.Width; this._resize.mxBottom = s.Height; } }, //現在のスタイルを取得する GetPos: function() { (this._layHandle) で{ 戻り値: 上: offsetTop、左: offsetLeft、幅: offsetWidth、高さ: offsetHeight } } }, //サイズを取得する GetSize: function(nowWidth, nowHeight, fixWidth, fixHeight) { var iWidth = nowWidth、iHeight = nowHeight、scale = iWidth / iHeight; //比率に応じて設定します if (fixHeight) { iWidth = (iHeight = fixHeight) * scale; } if(fixWidth && (!fixHeight || iWidth > fixWidth)){ iHeight = (iWidth = fixWidth) / スケール; } //サイズオブジェクトを返す return { 幅: iWidth, 高さ: iHeight } } } 以上がこの記事の全内容です。皆様の勉強のお役に立てれば幸いです。また、123WORDPRESS.COM を応援していただければ幸いです。 以下もご興味があるかもしれません:
|
<<: Centos7 での MySQL 5.7.20 のインストールと設定に関する詳細なチュートリアル
>>: SSH経由でローカルLinux仮想マシンに接続するプロセスを記録する
1. 基本仕様(1)InnoDBストレージエンジンを使用する必要があります。解釈:トランザクション、...
ケース1: 最後の提出とプッシュなし次のコマンドを実行します。 git コミット --amend g...
この記事では、例を使用して、MySQL 派生テーブルの簡単な使用方法を説明します。ご参考までに、詳細...
1. 準備ミドルウェア: Tomcat、Redis、Nginx Jar パッケージ: commons...
メタ属性には、name と http-equiv の 2 つがあります。 name 属性は主に、We...
ミラーを探すDocker Hubのウェブサイトからイメージを検索できます。Docker Hubのウェ...
CSS3 アニメーションで実現したシンプルでクールな効果。最終的な効果は次のようになります。 ページ...
多くの場合、画像をコンテナのサイズに合わせて調整する必要があります。 1. imgタグ方式幅と高さを...
問題: JDBCを使用してMySQLデータベースに接続すると、中国語の文字を挿入すると文字化けした文...
導入:スライダー ドラッグ検証は現在、多くの場所で使用されています。週末に 1 つ作成しようと思い、...
問題の説明:最近、rsyncで毎回同期するデータ量が多いが、データベースのbakファイルを保持する必...
ビューポートとはモバイル ブラウザは、Web ページを仮想の「ウィンドウ」(ビューポート) に配置し...
目次リアクティブ機能使用法: toRef 関数 (理解するだけ)使用法: ref関数レスポンシブデー...
ページ ヘッダーの固定レイアウトは、以前は position:fixed を使用して実装されていまし...
コンピュータ ルームのサーバー上の mysql がしばらく実行されていたのですが、突然、再起動しても...